os/kernelhwsrv/kerneltest/e32test/secure/t_capsup.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\secure\t_capsup.cpp
    15 // Overview:
    16 // Test support of platform security capabilities.
    17 // API Information:
    18 // TCapabilitySet, TSecurityInfo, TSecurityPolicy and various other methods.
    19 // Details:
    20 // - Verify the enumeration of each of the capability names.
    21 // - Test the TCapabilitySet class methods by setting and checking various
    22 // capabilities. Verify results are as expected.
    23 // - Test the TSecurityInfo class methods and verify results.
    24 // - Test the RProcess, RThread and RMessage2 SecureId() methods along with
    25 // User::CreatorSecureId(). Verify results are as expected.
    26 // - Test the RProcess, RThread and RMessage2 VendorId() methods along with
    27 // User::CreatorVendorId(). Verify results are as expected.
    28 // - Test the RProcess, RThread and RMessage2 HasCapability() methods along
    29 // with User::CreatorHasCapability(). Verify results are as expected.
    30 // - Test TSecurityPolicy constructors including macros for compile-time
    31 // construction. Verify results are as expected.
    32 // - Test kernel APIs.  Verify results are as expected.
    33 // - Test setting KernelConfigFlags.  Ensure that, if __PLATSEC_UNLOCKED__
    34 // is not set, the PlatSec flags cannot be unset.
    35 // - Test the use of platform security diagnostic strings.
    36 // Platforms/Drives/Compatibility:
    37 // All.
    38 // Assumptions/Requirement/Pre-requisites:
    39 // Failures and causes:
    40 // Base Port information:
    41 // 
    42 //
    43 
    44 #define __INCLUDE_CAPABILITY_NAMES__
    45 #define __E32TEST_EXTENSION__
    46 
    47 #include <e32cmn.h>
    48 #include <e32cmn_private.h>
    49 #include <u32exec.h>
    50 #include <e32test.h>
    51 #include <e32def.h>
    52 #include <e32def_private.h>
    53 #include "d_sldd.h"
    54 
    55 TBool PlatSecEnforcement;
    56 
    57 _LIT_SECURE_ID(KTestSecureId,0x101f534d);
    58 _LIT_SECURE_ID(KTestSecureId2,0x101f534e);
    59 _LIT_VENDOR_ID(KTestVendorId,0x01234567);
    60 _LIT_VENDOR_ID(KTestVendorId2,0x01234568);
    61 
    62 const TUint32 KTestCapabilities =(1<<ECapabilityTCB)
    63 								|(1<<ECapabilityPowerMgmt)
    64 								|(1<<ECapabilityReadDeviceData)
    65 								|(1<<ECapabilityDRM)
    66 								|(1<<ECapabilityProtServ)
    67 								|(1<<ECapabilityNetworkControl)
    68 								|(1<<ECapabilitySwEvent)
    69 								|(1<<ECapabilityLocalServices)
    70 								|(1<<ECapabilityWriteUserData)
    71 								|(1<<ECapabilitySurroundingsDD);
    72 
    73 LOCAL_D RTest test(_L("T_CAPSUP"));
    74 
    75 enum TTestProcessFunctions
    76 	{
    77 	ETestProcessServer,
    78 	};
    79 
    80 #include "testprocess.h"
    81 
    82 TInt StartServer();
    83 
    84 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
    85 	{
    86 	(void)aArg1;
    87 	(void)aArg2;
    88 
    89 	switch(aTestNum)
    90 		{
    91 
    92 	case ETestProcessServer:
    93 		return StartServer();
    94 
    95 	default:
    96 		User::Panic(_L("T_CAPSUP"),1);
    97 		}
    98 
    99 	return KErrNone;
   100 	}
   101 
   102 
   103 
   104 //
   105 // RTestThread
   106 //
   107 
   108 class RTestThread : public RThread
   109 	{
   110 public:
   111 	void Create(TThreadFunction aFunction,TAny* aArg=0);
   112 	};
   113 
   114 void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
   115 	{
   116 	TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
   117 	test(r==KErrNone);
   118 	}
   119 
   120 
   121 //
   122 // CTestSession
   123 //
   124 
   125 class CTestSession : public CSession2
   126 	{
   127 public:
   128 	enum {EShutdown,
   129 		ETestProcessSecurityInfo,ETestThreadSecurityInfo,ETestMessageSecurityInfo,ETestCreatorSecurityInfo,
   130 		ETestProcessSecureId,ETestThreadSecureId,ETestMessageSecureId,ETestCreatorSecureId,
   131 		ETestProcessVendorId,ETestThreadVendorId,ETestMessageVendorId,ETestCreatorVendorId,
   132 		ETestProcessHasCapability1,ETestProcessHasCapability2,
   133 		ETestThreadHasCapability1,ETestThreadHasCapability2,
   134 		ETestMessageHasCapability1,ETestMessageHasCapability2,
   135 		ETestMessageHasCapabilityL1,ETestMessageHasCapabilityL2,
   136 		ETestCreatorHasCapability1,ETestCreatorHasCapability2,
   137 		ETestSecurityPolicyAgainstMessage,
   138 		ETestSecurityPolicyAgainstCreator
   139 		};
   140 	enum {EPolicyCheckPassed = 15, EPolicyCheckFailed = 16};
   141 public:
   142 	CTestSession();
   143 	virtual void ServiceL(const RMessage2& aMessage);
   144 public:
   145 	};
   146 
   147 CTestSession::CTestSession()
   148 	: CSession2()
   149 	{}
   150 
   151 void CTestSession::ServiceL(const RMessage2& aMessage)
   152 	{
   153 	const RMessagePtr2 m(aMessage);
   154 	switch (aMessage.Function())
   155 		{
   156 		case CTestSession::ETestProcessSecureId:
   157 			{
   158 			RProcess process;
   159 			TInt r=process.Open(aMessage.Int0());
   160 			if(r==KErrNone)
   161 				{
   162 				r = process.SecureId();
   163 				process.Close();
   164 				}
   165 			else
   166 				r = KErrGeneral;
   167 			m.Complete(r);
   168 			}
   169 			break;
   170 
   171 		case CTestSession::ETestThreadSecureId:
   172 			{
   173 			RThread thread;
   174 			TInt r=thread.Open(aMessage.Int0());
   175 			if(r==KErrNone)
   176 				{
   177 				r = thread.SecureId();
   178 				thread.Close();
   179 				}
   180 			else
   181 				r = KErrGeneral;
   182 			m.Complete(r);
   183 			}
   184 			break;
   185 
   186 		case CTestSession::ETestMessageSecureId:
   187 			{
   188 			TInt32 id = m.SecureId();
   189 			m.Complete(id);
   190 			}
   191 			break;
   192 
   193 		case CTestSession::ETestCreatorSecureId:
   194 			{
   195 			m.Complete(User::CreatorSecureId());
   196 			}
   197 			break;
   198 
   199 		case CTestSession::ETestProcessVendorId:
   200 			{
   201 			RProcess process;
   202 			TInt r=process.Open(aMessage.Int0());
   203 			if(r==KErrNone)
   204 				{
   205 				r = process.VendorId();
   206 				process.Close();
   207 				}
   208 			else
   209 				r = KErrGeneral;
   210 			m.Complete(r);
   211 			}
   212 			break;
   213 
   214 		case CTestSession::ETestThreadVendorId:
   215 			{
   216 			RThread thread;
   217 			TInt r=thread.Open(aMessage.Int0());
   218 			if(r==KErrNone)
   219 				{
   220 				r = thread.VendorId();
   221 				thread.Close();
   222 				}
   223 			else
   224 				r = KErrGeneral;
   225 			m.Complete(r);
   226 			}
   227 			break;
   228 
   229 		case CTestSession::ETestMessageVendorId:
   230 			{
   231 			TInt32 id = m.VendorId();
   232 			m.Complete(id);
   233 			}
   234 			break;
   235 
   236 		case CTestSession::ETestCreatorVendorId:
   237 			{
   238 			m.Complete(User::CreatorVendorId());
   239 			}
   240 			break;
   241 
   242 		case CTestSession::ETestProcessSecurityInfo:
   243 			{
   244 			RProcess process;
   245 			TInt r=process.Open(aMessage.Int0());
   246 			if(r==KErrNone)
   247 				{
   248 				TSecurityInfo info;
   249 				info.Set(process);
   250 				r = m.Write(1,TPtrC8((TUint8*)&info,sizeof(info)));
   251 				process.Close();
   252 				}
   253 			else
   254 				r = KErrGeneral;
   255 			m.Complete(r);
   256 			}
   257 			break;
   258 
   259 		case CTestSession::ETestThreadSecurityInfo:
   260 			{
   261 			RThread thread;
   262 			TInt r=thread.Open(aMessage.Int0());
   263 			if(r==KErrNone)
   264 				{
   265 				TSecurityInfo info;
   266 				info.Set(thread);
   267 				r = m.Write(1,TPtrC8((TUint8*)&info,sizeof(info)));
   268 				thread.Close();
   269 				}
   270 			else
   271 				r = KErrGeneral;
   272 			m.Complete(r);
   273 			}
   274 			break;
   275 
   276 		case CTestSession::ETestMessageSecurityInfo:
   277 			{
   278 			TSecurityInfo info;
   279 			info.Set(m);
   280 			TInt r = m.Write(0,TPtrC8((TUint8*)&info,sizeof(info)));
   281 			m.Complete(r);
   282 			}
   283 			break;
   284 
   285 		case CTestSession::ETestCreatorSecurityInfo:
   286 			{
   287 			TSecurityInfo info;
   288 			info.SetToCreatorInfo();
   289 			TInt r = m.Write(0,TPtrC8((TUint8*)&info,sizeof(info)));
   290 			m.Complete(r);
   291 			}
   292 			break;
   293 
   294 		case CTestSession::ETestProcessHasCapability1:
   295 			{
   296 			RProcess process;
   297 			TInt r=process.Open(aMessage.Int0());
   298 			if(r==KErrNone)
   299 				{
   300 				r = process.HasCapability((TCapability)aMessage.Int1(),__PLATSEC_DIAGNOSTIC_STRING("ETestProcessHasCapability1"));
   301 				process.Close();
   302 				}
   303 			else
   304 				r = KErrGeneral;
   305 			m.Complete(r);
   306 			}
   307 			break;
   308 
   309 		case CTestSession::ETestProcessHasCapability2:
   310 			{
   311 			RProcess process;
   312 			TInt r=process.Open(aMessage.Int0());
   313 			if(r==KErrNone)
   314 				{
   315 				r = process.HasCapability((TCapability)aMessage.Int1(),(TCapability)aMessage.Int2(),__PLATSEC_DIAGNOSTIC_STRING("ETestProcessHasCapability2"));
   316 				process.Close();
   317 				}
   318 			else
   319 				r = KErrGeneral;
   320 			m.Complete(r);
   321 			}
   322 			break;
   323 
   324 		case CTestSession::ETestThreadHasCapability1:
   325 			{
   326 			RThread thread;
   327 			TInt r=thread.Open(aMessage.Int0());
   328 			if(r==KErrNone)
   329 				{
   330 				r = thread.HasCapability((TCapability)aMessage.Int1(),__PLATSEC_DIAGNOSTIC_STRING("ETestThreadHasCapability1"));
   331 				thread.Close();
   332 				}
   333 			else
   334 				r = KErrGeneral;
   335 			m.Complete(r);
   336 			}
   337 			break;
   338 
   339 		case CTestSession::ETestThreadHasCapability2:
   340 			{
   341 			RThread thread;
   342 			TInt r=thread.Open(aMessage.Int0());
   343 			if(r==KErrNone)
   344 				{
   345 				r = thread.HasCapability((TCapability)aMessage.Int1(),(TCapability)aMessage.Int2(),__PLATSEC_DIAGNOSTIC_STRING("ETestThreadHasCapability2"));
   346 				thread.Close();
   347 				}
   348 			else
   349 				r = KErrGeneral;
   350 			m.Complete(r);
   351 			}
   352 			break;
   353 
   354 		case CTestSession::ETestMessageHasCapability1:
   355 			{
   356 			TInt r = m.HasCapability((TCapability)aMessage.Int0(),__PLATSEC_DIAGNOSTIC_STRING("ETestMessageHasCapability1"));
   357 			m.Complete(r);
   358 			}
   359 			break;
   360 
   361 		case CTestSession::ETestMessageHasCapabilityL1:
   362 			{
   363 			TRAPD(r,m.HasCapabilityL((TCapability)aMessage.Int0(),__PLATSEC_DIAGNOSTIC_STRING("ETestMessageHasCapabilityL1")))
   364 			m.Complete(r);
   365 			}
   366 			break;
   367 
   368 		case CTestSession::ETestMessageHasCapability2:
   369 			{
   370 			TInt r = m.HasCapability((TCapability)aMessage.Int0(),(TCapability)aMessage.Int1(),__PLATSEC_DIAGNOSTIC_STRING("ETestMessageHasCapability2"));
   371 			m.Complete(r);
   372 			}
   373 			break;
   374 
   375 		case CTestSession::ETestMessageHasCapabilityL2:
   376 			{
   377 			TRAPD(r,m.HasCapabilityL((TCapability)aMessage.Int0(),(TCapability)aMessage.Int1(),__PLATSEC_DIAGNOSTIC_STRING("ETestMessageHasCapabilityL2")))
   378 			m.Complete(r);
   379 			}
   380 			break;
   381 
   382 		case CTestSession::ETestCreatorHasCapability1:
   383 			{
   384 			TInt r = User::CreatorHasCapability((TCapability)aMessage.Int0(),__PLATSEC_DIAGNOSTIC_STRING("ETestCreatorHasCapability1"));
   385 			m.Complete(r);
   386 			}
   387 			break;
   388 
   389 		case CTestSession::ETestCreatorHasCapability2:
   390 			{
   391 			TInt r = User::CreatorHasCapability((TCapability)aMessage.Int0(),(TCapability)aMessage.Int1(),__PLATSEC_DIAGNOSTIC_STRING("ETestCreatorHasCapability2"));
   392 			m.Complete(r);
   393 			}
   394 			break;
   395 
   396 		case CTestSession::ETestSecurityPolicyAgainstMessage:
   397 			{
   398 			TBuf8<sizeof(TSecurityPolicy)> buf(0);
   399 			TInt len = m.GetDesLength(0);
   400 			TInt r = KErrArgument;
   401 			if(len>0 && len <=buf.MaxSize())
   402 				{
   403 				r = m.Read(0, buf, 0);
   404 				if(r==KErrNone)
   405 					{
   406 					TSecurityPolicy policy;
   407 					r = policy.Set(buf);
   408 					if(r == KErrNone)
   409 						{
   410 						r = policy.CheckPolicy(m, __PLATSEC_DIAGNOSTIC_STRING("Testing message against policy -- sample additional diagnostic."));
   411 						if(r)
   412 							r = EPolicyCheckPassed;
   413 						else
   414 							r = EPolicyCheckFailed;
   415 						}
   416 					}
   417 				}
   418 			m.Complete(r);
   419 			break;
   420 			}
   421 
   422 		case CTestSession::ETestSecurityPolicyAgainstCreator:
   423 			{
   424 			TBuf8<sizeof(TSecurityPolicy)> buf(0);
   425 			TInt len = m.GetDesLength(0);
   426 			TInt r = KErrArgument;
   427 			if(len>0 && len <=buf.MaxSize())
   428 				{
   429 				r = m.Read(0, buf, 0);
   430 				if(r==KErrNone)
   431 					{
   432 					TSecurityPolicy policy;
   433 					r = policy.Set(buf);
   434 					if(r == KErrNone)
   435 						{
   436 						r = policy.CheckPolicyCreator(__PLATSEC_DIAGNOSTIC_STRING("Testing creator against policy -- sample additional diagnostic."));
   437 						if(r)
   438 							r = EPolicyCheckPassed;
   439 						else
   440 							r = EPolicyCheckFailed;
   441 						}
   442 					}
   443 				}
   444 			m.Complete(r);
   445 			break;
   446 			}
   447 
   448 		case CTestSession::EShutdown:
   449 			CActiveScheduler::Stop();
   450 			break;
   451 
   452 		default:
   453 			m.Complete(KErrNotSupported);
   454 			break;
   455 		}
   456 	}
   457 
   458 
   459 
   460 //
   461 // CTestServer
   462 //
   463 
   464 class CTestServer : public CServer2
   465 	{
   466 public:
   467 	CTestServer(TInt aPriority);
   468 	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
   469 	};
   470 
   471 CTestServer::CTestServer(TInt aPriority)
   472 	: CServer2(aPriority)
   473 	{
   474 	}
   475 
   476 CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const
   477 	{
   478 	return new (ELeave) CTestSession();
   479 	}
   480 
   481 
   482 
   483 //
   484 // CTestActiveScheduler
   485 //
   486 
   487 class CTestActiveScheduler : public CActiveScheduler
   488 	{
   489 public:
   490 	virtual void Error(TInt anError) const;
   491 	};
   492 
   493 void CTestActiveScheduler::Error(TInt anError) const
   494 	{
   495 	User::Panic(_L("TestServer Error"),anError);
   496 	}
   497 
   498 
   499 
   500 //
   501 // Server thread
   502 //
   503 
   504 _LIT(KServerName,"T_CAPSUP-server");
   505 const TInt KServerRendezvous = KRequestPending+1;
   506 
   507 void DoStartServer()
   508 	{
   509 	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
   510 	CActiveScheduler::Install(activeScheduler);
   511 	CleanupStack::PushL(activeScheduler);
   512 
   513 	CTestServer* server = new (ELeave) CTestServer(0);
   514 	CleanupStack::PushL(server);
   515 
   516 	User::LeaveIfError(server->Start(KServerName));
   517 
   518 	RProcess::Rendezvous(KServerRendezvous);
   519 
   520 	CActiveScheduler::Start();
   521 
   522 	CleanupStack::PopAndDestroy(2);
   523 	}
   524 
   525 TInt StartServer()
   526 	{
   527 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
   528 	if(!cleanupStack)
   529 		return KErrNoMemory;
   530 	TRAPD(leaveError,DoStartServer())
   531 	delete cleanupStack;
   532 	return leaveError;
   533 	}
   534 
   535 
   536 
   537 //
   538 // RTestSession
   539 //
   540 
   541 class RTestSession : public RSessionBase
   542 	{
   543 public:
   544 	inline TInt Connect()
   545 		{ return CreateSession(KServerName,TVersion());}
   546 	inline TInt Send(TInt aFunction)
   547 		{ return RSessionBase::SendReceive(aFunction); }
   548 	inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
   549 		{ return RSessionBase::SendReceive(aFunction,aArgs); }
   550 	inline void Send(TInt aFunction,TRequestStatus& aStatus)
   551 		{ RSessionBase::SendReceive(aFunction,aStatus); }
   552 	inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
   553 		{ RSessionBase::SendReceive(aFunction,aArgs,aStatus); }
   554 	};
   555 
   556 
   557 
   558 RTestSession Session;
   559 
   560 void CheckCapabilitySetEqual(const TCapabilitySet& a1,const TCapabilitySet& a2)
   561 	{
   562 	TInt i;
   563 	for(i=0; i<ECapability_Limit; i++)
   564 		test((!a1.HasCapability((TCapability)i))==(!a2.HasCapability((TCapability)i)));
   565 	}
   566 
   567 void TestCapabilitySet()
   568 	{
   569 	TCapabilitySet s;
   570 	TCapabilitySet all;
   571 	TCapabilitySet empty;
   572 	TInt i,j,k;
   573 
   574 	test.Start(_L("Each test stage also implicity tests HasCapability()"));
   575 
   576 	test.Next(_L("Test SetEmpty()"));
   577 	memset(&empty,-1,sizeof(empty));
   578 	empty.SetEmpty();
   579 	for(i=0; i<ECapability_HardLimit; i++)
   580 		test(!empty.HasCapability((TCapability)i));
   581 	// test memory cleared - assumes knowledge of internal representation
   582 	for(i=0; i<(TInt)sizeof(empty); ++i)
   583 		test(((TUint8*)&empty)[i] == 0);
   584 
   585 	test.Next(_L("Test SetAllSupported()"));
   586 	all.SetAllSupported();
   587 	// This code makes the assumption that there are no gaps in the TCapability enumeration
   588 	for(i=0; i<ECapability_Limit; i++)
   589 		test(all.HasCapability((TCapability)i));
   590 	for(; i<ECapability_HardLimit; i++)
   591 		test(!all.HasCapability((TCapability)i));
   592 
   593 	test.Next(_L("Test Set(TCapability)"));
   594 	for(j=-2; j<ECapability_HardLimit; j++)
   595 		{
   596 		if(j&1)
   597 			s.SetAllSupported();
   598 		else
   599 			s.SetEmpty();
   600 		s.Set((TCapability)j);
   601 		for(i=0; i<=ECapability_HardLimit; i++)
   602 			{
   603 			if(i==j)
   604 				test(!s.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   605 			else
   606 				test(!s.HasCapability((TCapability)i));
   607 			
   608 			}
   609 		test(!s.HasCapability(ECapability_Denied));
   610 		test(s.HasCapability(ECapability_None));
   611 		}
   612 
   613 	test.Next(_L("Test Set(TCapability,TCapability)"));
   614 	for(k=-2; k<ECapability_HardLimit; k++)
   615 		{
   616 		for(j=-2; j<ECapability_HardLimit; j++)
   617 			{
   618 			if((j^k)&1)
   619 				s.SetAllSupported();
   620 			else
   621 				s.SetEmpty();
   622 			s.Set((TCapability)j,(TCapability)k);
   623 			for(i=0; i<=ECapability_HardLimit; i++)
   624 				{
   625 				if(i==j || i==k)
   626 					test(!s.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   627 				else
   628 					test(!s.HasCapability((TCapability)i));
   629 
   630 				}
   631 			test(!s.HasCapability(ECapability_Denied));
   632 			test(s.HasCapability(ECapability_None));
   633 			}
   634 		}
   635 
   636 	test.Next(_L("Test TCapability(TCapability)"));
   637 	for(j=-2; j<ECapability_HardLimit; j++)
   638 		{
   639 		TCapabilitySet t((TCapability)j);
   640 		for(i=0; i<=ECapability_HardLimit; i++)
   641 			{
   642 			if(i==j)
   643 				test(!t.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   644 			else
   645 				test(!t.HasCapability((TCapability)i));
   646 
   647 			}
   648 		test(!s.HasCapability(ECapability_Denied));
   649 		test(s.HasCapability(ECapability_None));
   650 		}
   651 
   652 	test.Next(_L("Test TCapability(TCapability,TCapability)"));
   653 	for(k=-2; k<ECapability_HardLimit; k++)
   654 		{
   655 		for(j=-2; j<ECapability_HardLimit; j++)
   656 			{
   657 			TCapabilitySet t((TCapability)j,(TCapability)k);
   658 			for(i=0; i<=ECapability_HardLimit; i++)
   659 				{
   660 				if(i==j || i==k)
   661 					test(!t.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   662 				else
   663 					test(!t.HasCapability((TCapability)i));
   664 
   665 				}
   666 			test(!s.HasCapability(ECapability_Denied));
   667 			test(s.HasCapability(ECapability_None));
   668 			}
   669 		}
   670 
   671 	test.Next(_L("Test AddCapability(TCapability)"));
   672 	s.SetEmpty();
   673 	for(j=-2; j<ECapability_HardLimit; j++) // Add each capability in sequence
   674 		{
   675 		s.AddCapability((TCapability)j);
   676 		for(i=0; i<=j; i++)
   677 			test(!s.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   678 		for(; i<ECapability_HardLimit; i++)
   679 			test(!s.HasCapability((TCapability)i));
   680 		test(!s.HasCapability(ECapability_Denied));
   681 		test(s.HasCapability(ECapability_None));
   682 		}
   683 	s.SetEmpty();
   684 	for(j=ECapability_HardLimit-1; j>=-2; j--) // Add each capability in reverse sequence
   685 		{
   686 		s.AddCapability((TCapability)j);
   687 		for(i=ECapability_HardLimit-1; i>=j && i>=0; i--)
   688 			test(!s.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   689 		for(; i>=0; i--)
   690 			test(!s.HasCapability((TCapability)i));
   691 		test(!s.HasCapability(ECapability_Denied));
   692 		test(s.HasCapability(ECapability_None));
   693 		}
   694 
   695 	test.Next(_L("Test RemoveCapability(TCapability)"));
   696 	s.SetAllSupported();
   697 	for(j=-2; j<ECapability_HardLimit; j++) // Remove each capability in sequence
   698 		{
   699 		s.RemoveCapability((TCapability)j);
   700 		for(i=0; i<=j; i++)
   701 			test(!s.HasCapability((TCapability)i));
   702 		for(; i<ECapability_HardLimit; i++)
   703 			test(!s.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   704 		test(!s.HasCapability(ECapability_Denied));
   705 		test(s.HasCapability(ECapability_None));
   706 		}
   707 	s.SetAllSupported();
   708 	for(j=ECapability_HardLimit-1; j>=-2; j--) // Remove each capability reverse sequence
   709 		{
   710 		s.RemoveCapability((TCapability)j);
   711 		for(i=ECapability_HardLimit-1; i>=j && i >=0; i--)
   712 			test(!s.HasCapability((TCapability)i));
   713 		for(; i>=0; i--)
   714 			test(!s.HasCapability((TCapability)i) == !all.HasCapability((TCapability)i)); // Should have capability (if in set of All capabilities)
   715 		test(!s.HasCapability(ECapability_Denied));
   716 		test(s.HasCapability(ECapability_None));
   717 		}
   718 
   719 	test.Next(_L("Building test sets"));
   720 	TCapabilitySet even;
   721 	even.SetEmpty();
   722 	for(i=0; i<ECapability_Limit; i+=2) even.AddCapability((TCapability)i);
   723 	TCapabilitySet odd;
   724 	odd.SetEmpty();
   725 	for(i=1; i<ECapability_Limit; i+=2) odd.AddCapability((TCapability)i);
   726 
   727 	test.Next(_L("Test Union(TCapabilitySet)"));
   728 	s.SetEmpty();
   729 	CheckCapabilitySetEqual(s,empty);
   730 	s.Union(odd);
   731 	CheckCapabilitySetEqual(s,odd);
   732 	s.Union(odd);
   733 	CheckCapabilitySetEqual(s,odd);
   734 	s.Union(empty);
   735 	CheckCapabilitySetEqual(s,odd);
   736 	s.Union(even);
   737 	CheckCapabilitySetEqual(s,all);
   738 	s.Union(even);
   739 	CheckCapabilitySetEqual(s,all);
   740 	s.SetEmpty();
   741 	CheckCapabilitySetEqual(s,empty);
   742 	s.Union(even);
   743 	CheckCapabilitySetEqual(s,even);
   744 	s.Union(even);
   745 	CheckCapabilitySetEqual(s,even);
   746 	s.Union(empty);
   747 	CheckCapabilitySetEqual(s,even);
   748 	s.Union(odd);
   749 	CheckCapabilitySetEqual(s,all);
   750 	s.Union(odd);
   751 	CheckCapabilitySetEqual(s,all);
   752 
   753 	test.Next(_L("Test Intersection(TCapabilitySet)"));
   754 	s.SetAllSupported();
   755 	CheckCapabilitySetEqual(s,all);
   756 	s.Intersection(odd);
   757 	CheckCapabilitySetEqual(s,odd);
   758 	s.Intersection(odd);
   759 	CheckCapabilitySetEqual(s,odd);
   760 	s.Intersection(even);
   761 	CheckCapabilitySetEqual(s,empty);
   762 	s.Intersection(even);
   763 	CheckCapabilitySetEqual(s,empty);
   764 	s.SetAllSupported();
   765 	CheckCapabilitySetEqual(s,all);
   766 	s.Intersection(even);
   767 	CheckCapabilitySetEqual(s,even);
   768 	s.Intersection(even);
   769 	CheckCapabilitySetEqual(s,even);
   770 	s.Intersection(odd);
   771 	CheckCapabilitySetEqual(s,empty);
   772 	s.Intersection(odd);
   773 	CheckCapabilitySetEqual(s,empty);
   774 
   775 	test.Next(_L("Test Remove(TCapabilitySet)"));
   776 	s.SetAllSupported();
   777 	CheckCapabilitySetEqual(s,all);
   778 	s.Remove(odd);
   779 	CheckCapabilitySetEqual(s,even);
   780 	s.Remove(odd);
   781 	CheckCapabilitySetEqual(s,even);
   782 	s.Remove(empty);
   783 	CheckCapabilitySetEqual(s,even);
   784 	s.Remove(even);
   785 	CheckCapabilitySetEqual(s,empty);
   786 	s.Remove(even);
   787 	CheckCapabilitySetEqual(s,empty);
   788 	s.SetAllSupported();
   789 	CheckCapabilitySetEqual(s,all);
   790 	s.Remove(even);
   791 	CheckCapabilitySetEqual(s,odd);
   792 	s.Remove(even);
   793 	CheckCapabilitySetEqual(s,odd);
   794 	s.Remove(empty);
   795 	CheckCapabilitySetEqual(s,odd);
   796 	s.Remove(odd);
   797 	CheckCapabilitySetEqual(s,empty);
   798 	s.Remove(odd);
   799 	CheckCapabilitySetEqual(s,empty);
   800 
   801 	test.Next(_L("Test HasCapabilities(TCapabilitySet)"));
   802 	for(i=0; i<ECapability_Limit; i++)
   803 		{
   804 		if(!all.HasCapability((TCapability)i))
   805 			continue;
   806 		TCapabilitySet t((TCapability)i);
   807 		test(t.HasCapabilities(t));
   808 		test(all.HasCapabilities(t));
   809 		test(!empty.HasCapabilities(t));
   810 		if(i&1)
   811 			{
   812 			test(odd.HasCapabilities(t));
   813 			test(!even.HasCapabilities(t));
   814 			}
   815 		else
   816 			{
   817 			test(!odd.HasCapabilities(t));
   818 			test(even.HasCapabilities(t));
   819 			}
   820 		test(!t.HasCapabilities(all));
   821 		test(!t.HasCapabilities(even));
   822 		test(!t.HasCapabilities(odd));
   823 		test(t.HasCapabilities(empty));
   824 		}
   825 
   826 	test(all.HasCapabilities(all));
   827 	test(all.HasCapabilities(even));
   828 	test(all.HasCapabilities(odd));
   829 	test(all.HasCapabilities(empty));
   830 
   831 	test(!even.HasCapabilities(all));
   832 	test(even.HasCapabilities(even));
   833 	test(!even.HasCapabilities(odd));
   834 	test(even.HasCapabilities(empty));
   835 
   836 	test(!odd.HasCapabilities(all));
   837 	test(!odd.HasCapabilities(even));
   838 	test(odd.HasCapabilities(odd));
   839 	test(odd.HasCapabilities(empty));
   840 
   841 	test(!empty.HasCapabilities(all));
   842 	test(!empty.HasCapabilities(even));
   843 	test(!empty.HasCapabilities(odd));
   844 	test(empty.HasCapabilities(empty));
   845 
   846 	test.End();
   847 	}
   848 
   849 void CheckSecurityInfo(const TSecurityInfo& a1,const TSecurityInfo& a2)
   850 	{
   851 	test(a1.iSecureId==a2.iSecureId);
   852 	test(a1.iVendorId==a2.iVendorId);
   853 	TInt i;
   854 	for(i=0; i<ECapability_Limit; i++)
   855 		test((!a1.iCaps.HasCapability((TCapability)i))==(!a2.iCaps.HasCapability((TCapability)i)));
   856 	}
   857 
   858 void TestSecurityInfo()
   859 	{
   860 	TSecurityInfo self;
   861 	TSecurityInfo info;
   862 	TPckg<TSecurityInfo> infoPtr(info);
   863 	TInt i;
   864 
   865 	test.Start(_L("Test TSecurityInfo::SetToCurrentInfo"));
   866 	self.SetToCurrentInfo();
   867 	test(self.iSecureId==KTestSecureId);
   868 	test(self.iVendorId==KTestVendorId);
   869 	for(i=0; i<ECapability_Limit; i++)
   870 		test((TUint32)(self.iCaps.HasCapability((TCapability)i)!=0)==((KTestCapabilities>>i)&1));
   871 
   872 	test.Next(_L("Test TSecurityInfo::Set(RProcess)"));
   873 	Mem::FillZ(&info,sizeof(info));
   874 	TInt32 r = Session.Send(CTestSession::ETestProcessSecurityInfo,TIpcArgs(TUint(RProcess().Id()),&infoPtr));
   875 	test(r==KErrNone);
   876 	CheckSecurityInfo(self,info);
   877 
   878 	test.Next(_L("Test TSecurityInfo::Set(RThread)"));
   879 	Mem::FillZ(&info,sizeof(info));
   880 	r = Session.Send(CTestSession::ETestThreadSecurityInfo,TIpcArgs(TUint(RThread().Id()),&infoPtr));
   881 	test(r==KErrNone);
   882 	CheckSecurityInfo(self,info);
   883 
   884 	test.Next(_L("Test TSecurityInfo::Set(RMessagePtr2)"));
   885 	Mem::FillZ(&info,sizeof(info));
   886 	r = Session.Send(CTestSession::ETestMessageSecurityInfo,TIpcArgs(&infoPtr));
   887 	test(r==KErrNone);
   888 	CheckSecurityInfo(self,info);
   889 
   890 	test.Next(_L("Test TSecurityInfo::SetToCreatorInfo"));
   891 	Mem::FillZ(&info,sizeof(info));
   892 	r = Session.Send(CTestSession::ETestCreatorSecurityInfo,TIpcArgs(&infoPtr));
   893 	test(r==KErrNone);
   894 	CheckSecurityInfo(self,info);
   895 
   896 	test.End();
   897 	}
   898 
   899 void TestSecureId()
   900 	{
   901 	test.Start(_L("Test RProcess::SecureId()"));
   902 	TInt r = Session.Send(CTestSession::ETestProcessSecureId,TIpcArgs(TUint(RProcess().Id())));
   903 	test((TUint32)r==KTestSecureId);
   904 
   905 	test.Next(_L("Test RThread::SecureId()"));
   906 	r = Session.Send(CTestSession::ETestThreadSecureId,TIpcArgs(TUint(RThread().Id())));
   907 	test((TUint32)r==KTestSecureId);
   908 
   909 	test.Next(_L("Test RMessage2::SecureId()"));
   910 	r = Session.Send(CTestSession::ETestMessageSecureId);
   911 	test((TUint32)r==KTestSecureId);
   912 
   913 	test.Next(_L("Test User::CreatorSecureId()"));
   914 	r = Session.Send(CTestSession::ETestCreatorSecureId);
   915 	test((TUint32)r==KTestSecureId);
   916 
   917 	test.End();
   918 	}
   919 
   920 void TestVendorId()
   921 	{
   922 	test.Start(_L("Test RProcess::VendorId()"));
   923 	TInt r = Session.Send(CTestSession::ETestProcessVendorId,TIpcArgs(TUint(RProcess().Id())));
   924 	test((TUint32)r==KTestVendorId);
   925 
   926 	test.Next(_L("Test RThread::VendorId()"));
   927 	r = Session.Send(CTestSession::ETestThreadVendorId,TIpcArgs(TUint(RThread().Id())));
   928 	test((TUint32)r==KTestVendorId);
   929 
   930 	test.Next(_L("Test RMessage2::VendorId()"));
   931 	r = Session.Send(CTestSession::ETestMessageVendorId);
   932 	test((TUint32)r==KTestVendorId);
   933 
   934 	test.Next(_L("Test User::CreatorVendorId()"));
   935 	r = Session.Send(CTestSession::ETestCreatorVendorId);
   936 	test((TUint32)r==KTestVendorId);
   937 
   938 	test.End();
   939 	}
   940 
   941 void TestHasCapability()
   942 	{
   943 	TInt failResult=PlatSecEnforcement ? 0 : 1;
   944 	TInt failResultL=PlatSecEnforcement ? KErrPermissionDenied : KErrNone;
   945 
   946 	test.Start(_L("Test RProcess::HasCapability(TCapability)"));
   947 	TInt r = Session.Send(CTestSession::ETestProcessHasCapability1,TIpcArgs(TUint(RProcess().Id()),ECapabilityLocalServices));
   948 	test(r);
   949 	r = Session.Send(CTestSession::ETestProcessHasCapability1,TIpcArgs(TUint(RProcess().Id()),ECapabilityNetworkServices));
   950 	test(r==failResult);
   951 	r = Session.Send(CTestSession::ETestProcessHasCapability1,TIpcArgs(TUint(RProcess().Id()),ECapability_None));
   952 	test(r);
   953 	r = Session.Send(CTestSession::ETestProcessHasCapability1,TIpcArgs(TUint(RProcess().Id()),ECapability_Denied));
   954 	test(r==failResult);
   955 
   956 	test.Next(_L("Test RProcess::HasCapability(TCapability,TCapability)"));
   957 	r = Session.Send(CTestSession::ETestProcessHasCapability2,TIpcArgs(TUint(RProcess().Id()),ECapabilityLocalServices,ECapabilityWriteUserData));
   958 	test(r);
   959 	r = Session.Send(CTestSession::ETestProcessHasCapability2,TIpcArgs(TUint(RProcess().Id()),ECapabilityNetworkServices,ECapabilityWriteUserData));
   960 	test(r==failResult);
   961 	r = Session.Send(CTestSession::ETestProcessHasCapability2,TIpcArgs(TUint(RProcess().Id()),ECapabilityLocalServices,ECapabilityReadUserData));
   962 	test(r==failResult);
   963 	r = Session.Send(CTestSession::ETestProcessHasCapability2,TIpcArgs(TUint(RProcess().Id()),ECapabilityNetworkServices,ECapabilityReadUserData));
   964 	test(r==failResult);
   965 	r = Session.Send(CTestSession::ETestProcessHasCapability2,TIpcArgs(TUint(RProcess().Id()),ECapability_None,ECapabilityWriteUserData));
   966 	test(r);
   967 	r = Session.Send(CTestSession::ETestProcessHasCapability2,TIpcArgs(TUint(RProcess().Id()),ECapability_Denied,ECapabilityWriteUserData));
   968 	test(r==failResult);
   969 
   970 	test.Next(_L("Test RThread::HasCapability(TCapability)"));
   971 	r = Session.Send(CTestSession::ETestThreadHasCapability1,TIpcArgs(TUint(RThread().Id()),ECapabilityLocalServices));
   972 	test(r);
   973 	r = Session.Send(CTestSession::ETestThreadHasCapability1,TIpcArgs(TUint(RThread().Id()),ECapabilityNetworkServices));
   974 	test(r==failResult);
   975 	r = Session.Send(CTestSession::ETestThreadHasCapability1,TIpcArgs(TUint(RThread().Id()),ECapability_None));
   976 	test(r);
   977 	r = Session.Send(CTestSession::ETestThreadHasCapability1,TIpcArgs(TUint(RThread().Id()),ECapability_Denied));
   978 	test(r==failResult);
   979 
   980 	test.Next(_L("Test RThread::HasCapability(TCapability,TCapability)"));
   981 	r = Session.Send(CTestSession::ETestThreadHasCapability2,TIpcArgs(TUint(RThread().Id()),ECapabilityLocalServices,ECapabilityWriteUserData));
   982 	test(r);
   983 	r = Session.Send(CTestSession::ETestThreadHasCapability2,TIpcArgs(TUint(RThread().Id()),ECapabilityNetworkServices,ECapabilityWriteUserData));
   984 	test(r==failResult);
   985 	r = Session.Send(CTestSession::ETestThreadHasCapability2,TIpcArgs(TUint(RThread().Id()),ECapabilityLocalServices,ECapabilityReadUserData));
   986 	test(r==failResult);
   987 	r = Session.Send(CTestSession::ETestThreadHasCapability2,TIpcArgs(TUint(RThread().Id()),ECapabilityNetworkServices,ECapabilityReadUserData));
   988 	test(r==failResult);
   989 	r = Session.Send(CTestSession::ETestThreadHasCapability2,TIpcArgs(TUint(RThread().Id()),ECapability_None,ECapabilityWriteUserData));
   990 	test(r);
   991 	r = Session.Send(CTestSession::ETestThreadHasCapability2,TIpcArgs(TUint(RThread().Id()),ECapability_Denied,ECapabilityWriteUserData));
   992 	test(r==failResult);
   993 
   994 	test.Next(_L("Test RMessagePtr2::HasCapability(TCapability)"));
   995 	r = Session.Send(CTestSession::ETestMessageHasCapability1,TIpcArgs(ECapabilityLocalServices));
   996 	test(r);
   997 	r = Session.Send(CTestSession::ETestMessageHasCapability1,TIpcArgs(ECapabilityNetworkServices));
   998 	test(r==failResult);
   999 	r = Session.Send(CTestSession::ETestMessageHasCapability1,TIpcArgs(ECapability_None));
  1000 	test(r);
  1001 	r = Session.Send(CTestSession::ETestMessageHasCapability1,TIpcArgs(ECapability_Denied));
  1002 	test(r==failResult);
  1003 
  1004 	test.Next(_L("Test RMessagePtr2::HasCapabilityL(TCapability)"));
  1005 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL1,TIpcArgs(ECapabilityLocalServices));
  1006 	test(r==KErrNone);
  1007 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL1,TIpcArgs(ECapabilityNetworkServices));
  1008 	test(r==failResultL);
  1009 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL1,TIpcArgs(ECapability_None));
  1010 	test(r==KErrNone);
  1011 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL1,TIpcArgs(ECapability_Denied));
  1012 	test(r==failResultL);
  1013 
  1014 	test.Next(_L("Test RMessagePtr2::HasCapability(TCapability,TCapability)"));
  1015 	r = Session.Send(CTestSession::ETestMessageHasCapability2,TIpcArgs(ECapabilityLocalServices,ECapabilityWriteUserData));
  1016 	test(r);
  1017 	r = Session.Send(CTestSession::ETestMessageHasCapability2,TIpcArgs(ECapabilityNetworkServices,ECapabilityWriteUserData));
  1018 	test(r==failResult);
  1019 	r = Session.Send(CTestSession::ETestMessageHasCapability2,TIpcArgs(ECapabilityLocalServices,ECapabilityReadUserData));
  1020 	test(r==failResult);
  1021 	r = Session.Send(CTestSession::ETestMessageHasCapability2,TIpcArgs(ECapabilityNetworkServices,ECapabilityReadUserData));
  1022 	test(r==failResult);
  1023 	r = Session.Send(CTestSession::ETestMessageHasCapability2,TIpcArgs(ECapability_None,ECapabilityWriteUserData));
  1024 	test(r);
  1025 	r = Session.Send(CTestSession::ETestMessageHasCapability2,TIpcArgs(ECapability_Denied,ECapabilityWriteUserData));
  1026 	test(r==failResult);
  1027 
  1028 	test.Next(_L("Test RMessagePtr2::HasCapabilityL(TCapability,TCapability)"));
  1029 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL2,TIpcArgs(ECapabilityLocalServices,ECapabilityWriteUserData));
  1030 	test(r==KErrNone);
  1031 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL2,TIpcArgs(ECapabilityNetworkServices,ECapabilityWriteUserData));
  1032 	test(r==failResultL);
  1033 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL2,TIpcArgs(ECapabilityLocalServices,ECapabilityReadUserData));
  1034 	test(r==failResultL);
  1035 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL2,TIpcArgs(ECapabilityNetworkServices,ECapabilityReadUserData));
  1036 	test(r==failResultL);
  1037 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL2,TIpcArgs(ECapability_None,ECapabilityWriteUserData));
  1038 	test(r==KErrNone);
  1039 	r = Session.Send(CTestSession::ETestMessageHasCapabilityL2,TIpcArgs(ECapability_Denied,ECapabilityWriteUserData));
  1040 	test(r==failResultL);
  1041 
  1042 	test.Next(_L("Test User::CreatorHasCapability(TCapability)"));
  1043 	r = Session.Send(CTestSession::ETestCreatorHasCapability1,TIpcArgs(ECapabilityLocalServices));
  1044 	test(r);
  1045 	r = Session.Send(CTestSession::ETestCreatorHasCapability1,TIpcArgs(ECapabilityNetworkServices));
  1046 	test(r==failResult);
  1047 	r = Session.Send(CTestSession::ETestCreatorHasCapability1,TIpcArgs(ECapability_None));
  1048 	test(r);
  1049 	r = Session.Send(CTestSession::ETestCreatorHasCapability1,TIpcArgs(ECapability_Denied));
  1050 	test(r==failResult);
  1051 
  1052 	test.Next(_L("Test User::CreatorHasCapability(TCapability,TCapability)"));
  1053 	r = Session.Send(CTestSession::ETestCreatorHasCapability2,TIpcArgs(ECapabilityLocalServices,ECapabilityWriteUserData));
  1054 	test(r);
  1055 	r = Session.Send(CTestSession::ETestCreatorHasCapability2,TIpcArgs(ECapabilityNetworkServices,ECapabilityWriteUserData));
  1056 	test(r==failResult);
  1057 	r = Session.Send(CTestSession::ETestCreatorHasCapability2,TIpcArgs(ECapabilityLocalServices,ECapabilityReadUserData));
  1058 	test(r==failResult);
  1059 	r = Session.Send(CTestSession::ETestCreatorHasCapability2,TIpcArgs(ECapabilityNetworkServices,ECapabilityReadUserData));
  1060 	test(r==failResult);
  1061 	r = Session.Send(CTestSession::ETestCreatorHasCapability2,TIpcArgs(ECapability_None,ECapabilityWriteUserData));
  1062 	test(r);
  1063 	r = Session.Send(CTestSession::ETestCreatorHasCapability2,TIpcArgs(ECapability_Denied,ECapabilityWriteUserData));
  1064 	test(r==failResult);
  1065 
  1066 	test.End();
  1067 	}
  1068 
  1069 TBool SecurityPoliciesEqual(const TSecurityPolicy& a,const TSecurityPolicy& b)
  1070 	{
  1071 	return Mem::Compare((TUint8*)&a, sizeof(TSecurityPolicy), (TUint8*)&b, sizeof(TSecurityPolicy))==0;
  1072 	}
  1073 
  1074 void TestSecurityPolicy()
  1075 	{
  1076 	test.Start(_L("Test TSecurityPolicy Constructors"));
  1077 
  1078 	TBool failResult=!PlatSecEnforcement;
  1079 
  1080 	test.Next(_L("Empty Constructor"));
  1081 		{
  1082 		TSecurityPolicy empty;
  1083 		TPtrC8 ptr = empty.Package();
  1084 		TSecurityPolicy empty2;
  1085 		test(empty2.Set(ptr) == KErrNone);
  1086 		test(SecurityPoliciesEqual(empty, empty2));
  1087 
  1088 		test(failResult!=!empty.CheckPolicy(RProcess()));
  1089 		test(failResult!=!empty.CheckPolicy(RThread()));
  1090 		test(failResult!=!empty2.CheckPolicy(RProcess()));
  1091 		test(failResult!=!empty2.CheckPolicy(RThread()));
  1092 
  1093 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1094 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1095 
  1096 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1097 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1098 		}
  1099 
  1100 	test.Next(_L("Always Fail Constructor"));
  1101 		{
  1102 		TSecurityPolicy fail(TSecurityPolicy::EAlwaysFail);
  1103 		TPtrC8 ptr = fail.Package();
  1104 		TSecurityPolicy fail2;
  1105 		test(fail2.Set(ptr) == KErrNone);
  1106 		test(SecurityPoliciesEqual(fail, fail2));
  1107 
  1108 		test(failResult!=!fail.CheckPolicy(RProcess()));
  1109 		test(failResult!=!fail.CheckPolicy(RThread()));
  1110 		test(failResult!=!fail2.CheckPolicy(RProcess()));
  1111 		test(failResult!=!fail2.CheckPolicy(RThread()));
  1112 
  1113 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1114 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1115 
  1116 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1117 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1118 		}
  1119 
  1120 	test.Next(_L("Always Pass Constructor"));
  1121 		{
  1122 		TSecurityPolicy pass(TSecurityPolicy::EAlwaysPass);
  1123 		TPtrC8 ptr = pass.Package();
  1124 		TSecurityPolicy pass2;
  1125 		test(pass2.Set(ptr) == KErrNone);
  1126 		test(SecurityPoliciesEqual(pass, pass2));
  1127 
  1128 		test(pass.CheckPolicy(RProcess()));
  1129 		test(pass.CheckPolicy(RThread()));
  1130 		test(pass2.CheckPolicy(RProcess()));
  1131 		test(pass2.CheckPolicy(RThread()));
  1132 
  1133 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1134 		test(r==CTestSession::EPolicyCheckPassed);
  1135 
  1136 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1137 		test(r==CTestSession::EPolicyCheckPassed);
  1138 		}
  1139 
  1140 	test.Next(_L("3 Capability Constructor"));
  1141 		{
  1142 		TSecurityPolicy threeCaps(ECapabilityTCB,ECapabilityDRM,ECapabilityProtServ);
  1143 		//Current process has these three
  1144 		test(threeCaps.CheckPolicy(RProcess()));
  1145 		test(threeCaps.CheckPolicy(RThread()));
  1146 		TPtrC8 ptr = threeCaps.Package();
  1147 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1148 		test(r==CTestSession::EPolicyCheckPassed);
  1149 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1150 		test(r==CTestSession::EPolicyCheckPassed);
  1151 		}
  1152 
  1153 		{
  1154 		TSecurityPolicy threeCaps(ECapabilityTCB,ECapabilityProtServ,ECapabilityCommDD);
  1155 		//Current process doesn't have ECapabilityCommDD
  1156 		test(failResult!=!(threeCaps.CheckPolicy(RProcess())));
  1157 		test(failResult!=!(threeCaps.CheckPolicy(RThread())));
  1158 		TPtrC8 ptr = threeCaps.Package();
  1159 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1160 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1161 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1162 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1163 		}
  1164 
  1165 		{
  1166 		TSecurityPolicy threeCaps(ECapabilityCommDD);
  1167 		//Current process doesn't have ECapabilityCommDD
  1168 		test(failResult!=!(threeCaps.CheckPolicy(RProcess())));
  1169 		test(failResult!=!(threeCaps.CheckPolicy(RThread())));
  1170 		TPtrC8 ptr = threeCaps.Package();
  1171 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1172 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1173 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1174 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1175 		}
  1176 
  1177 		{
  1178 		TSecurityPolicy threeCaps(ECapabilityTCB);
  1179 		//Current process has TCB + 2 * ECapability_None
  1180 		test(threeCaps.CheckPolicy(RProcess()));
  1181 		test(threeCaps.CheckPolicy(RThread()));
  1182 
  1183 		TPtrC8 ptr = threeCaps.Package();
  1184 		TSecurityPolicy threeCaps2;
  1185 		TBuf8<sizeof(TSecurityPolicy)> invalid;
  1186 		for(TInt i=4; i<=7; i++)
  1187 			{
  1188 			invalid=ptr;
  1189 			invalid[i] = 0; // Set unused capability to be invalid
  1190 			test(threeCaps2.Set(invalid)==KErrArgument);
  1191 			}
  1192 		test(threeCaps2.Set(ptr)==KErrNone);
  1193 		test(SecurityPoliciesEqual(threeCaps, threeCaps2));
  1194 
  1195 		test(threeCaps2.CheckPolicy(RProcess()));
  1196 		test(threeCaps2.CheckPolicy(RThread()));
  1197 
  1198 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1199 		test(r==CTestSession::EPolicyCheckPassed);
  1200 
  1201 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1202 		test(r==CTestSession::EPolicyCheckPassed);
  1203 		}
  1204 
  1205 	test.Next(_L("7 Capability Constructor"));
  1206 		{
  1207 		TSecurityPolicy sevenCaps(ECapabilityTCB,ECapabilityWriteUserData,ECapabilitySwEvent,ECapabilityProtServ,ECapabilityNetworkControl,ECapabilityDRM,ECapabilityReadDeviceData);
  1208 		//Current process has all 7 of these.
  1209 		test(sevenCaps.CheckPolicy(RProcess()));
  1210 		test(sevenCaps.CheckPolicy(RThread()));
  1211 		TPtrC8 ptr = sevenCaps.Package();
  1212 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1213 		test(r==CTestSession::EPolicyCheckPassed);
  1214 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1215 		test(r==CTestSession::EPolicyCheckPassed);
  1216 		}
  1217 		
  1218 		{
  1219 		TSecurityPolicy sevenCaps(ECapabilityTCB,ECapabilityWriteUserData,ECapabilityMultimediaDD,ECapabilityProtServ,ECapabilityNetworkControl,ECapabilityDRM,ECapabilityReadDeviceData);
  1220 		//Current process doesn't have MultimediaDD
  1221 		test(failResult!=!(sevenCaps.CheckPolicy(RProcess())));
  1222 		test(failResult!=!(sevenCaps.CheckPolicy(RThread())));
  1223 		TPtrC8 ptr = sevenCaps.Package();
  1224 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1225 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1226 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1227 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1228 		}
  1229 
  1230 		{
  1231 		TSecurityPolicy sevenCaps(ECapabilityTCB,ECapabilityWriteUserData,ECapabilityMultimediaDD,ECapabilityProtServ,ECapabilityTrustedUI,ECapabilityDRM,ECapabilityReadDeviceData);
  1232 		//Current process doesn't have MultiMediaDD or TrustedUI
  1233 		test(failResult!=!(sevenCaps.CheckPolicy(RProcess())));
  1234 		test(failResult!=!(sevenCaps.CheckPolicy(RThread())));
  1235 
  1236 		TPtrC8 ptr = sevenCaps.Package();
  1237 		TSecurityPolicy sevenCaps2;
  1238 		test(sevenCaps2.Set(ptr)==KErrNone);
  1239 		test(SecurityPoliciesEqual(sevenCaps,sevenCaps2));
  1240 		test(failResult!=!(sevenCaps2.CheckPolicy(RProcess())));
  1241 		test(failResult!=!(sevenCaps2.CheckPolicy(RThread())));
  1242 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1243 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);;
  1244 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1245 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);;
  1246 		}
  1247 
  1248 	test.Next(_L("SID + 3 constructor"));
  1249 		{
  1250 		TSecurityPolicy sid(KTestSecureId, ECapabilityProtServ, ECapabilityDRM, ECapabilityReadDeviceData);
  1251 		//Current process has all three + sid
  1252 		test(sid.CheckPolicy(RProcess()));
  1253 		test(sid.CheckPolicy(RThread()));
  1254 
  1255 		TPtrC8 ptr = sid.Package();
  1256 		TSecurityPolicy sid2;
  1257 		test(sid2.Set(ptr)==KErrNone);
  1258 		test(SecurityPoliciesEqual(sid,sid2));
  1259 		test(sid2.CheckPolicy(RProcess()));
  1260 		test(sid2.CheckPolicy(RThread()));
  1261 
  1262 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1263 		test(r==CTestSession::EPolicyCheckPassed);
  1264 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1265 		test(r==CTestSession::EPolicyCheckPassed);
  1266 		}
  1267 
  1268 		{
  1269 		TSecurityPolicy sid(KTestSecureId2, ECapabilityProtServ, ECapabilityDRM, ECapabilityReadDeviceData);
  1270 		//Current process has all three caps but not sid
  1271 		test(failResult!=!(sid.CheckPolicy(RProcess())));
  1272 		test(failResult!=!(sid.CheckPolicy(RThread())));
  1273 		TPtrC8 ptr = sid.Package();
  1274 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1275 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1276 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1277 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1278 		}
  1279 
  1280 		{
  1281 		TSecurityPolicy sid(KTestSecureId, ECapabilityProtServ, ECapabilityWriteDeviceData);
  1282 		//Current process has sid but missing ECapabilityWriteDeviceData
  1283 		test(failResult!=!(sid.CheckPolicy(RProcess())));
  1284 		test(failResult!=!(sid.CheckPolicy(RThread())));
  1285 		TPtrC8 ptr = sid.Package();
  1286 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1287 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1288 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1289 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1290 		}
  1291 
  1292 		{
  1293 		TSecurityPolicy sid(KTestSecureId2, ECapabilityProtServ, ECapabilityWriteDeviceData);
  1294 		//Current process is missing sid and ECapabilityWriteDeviceData
  1295 		test(failResult!=!(sid.CheckPolicy(RProcess())));
  1296 		test(failResult!=!(sid.CheckPolicy(RThread())));
  1297 		TPtrC8 ptr = sid.Package();
  1298 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1299 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1300 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1301 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1302 		}
  1303 
  1304 	test.Next(_L("VID + 3 constructor"));
  1305 		{
  1306 		TSecurityPolicy vid(KTestVendorId, ECapabilityProtServ, ECapabilityDRM, ECapabilityReadDeviceData);
  1307 		//Current process has all three + vid
  1308 		test(vid.CheckPolicy(RProcess()));
  1309 		test(vid.CheckPolicy(RThread()));
  1310 
  1311 		TPtrC8 ptr = vid.Package();
  1312 		TSecurityPolicy vid2;
  1313 		test(vid2.Set(ptr)==KErrNone);
  1314 		test(SecurityPoliciesEqual(vid,vid2));
  1315 		test(vid2.CheckPolicy(RProcess()));
  1316 		test(vid2.CheckPolicy(RThread()));
  1317 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1318 		test(r==CTestSession::EPolicyCheckPassed);
  1319 		}
  1320 
  1321 		{
  1322 		TSecurityPolicy vid(KTestVendorId2, ECapabilityProtServ, ECapabilityDRM, ECapabilityReadDeviceData);
  1323 		//Current process has all three caps but not vid
  1324 		test(failResult!=!(vid.CheckPolicy(RProcess())));
  1325 		test(failResult!=!(vid.CheckPolicy(RThread())));
  1326 		TPtrC8 ptr = vid.Package();
  1327 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1328 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1329 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1330 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1331 		}
  1332 
  1333 		{
  1334 		TSecurityPolicy vid(KTestVendorId, ECapabilityProtServ, ECapabilityWriteDeviceData);
  1335 		//Current process has vid but missing ECapabilityWriteDeviceData
  1336 		test(failResult!=!(vid.CheckPolicy(RProcess())));
  1337 		test(failResult!=!(vid.CheckPolicy(RThread())));
  1338 		TPtrC8 ptr = vid.Package();
  1339 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1340 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1341 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1342 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1343 		}
  1344 
  1345 		{
  1346 		TSecurityPolicy vid(KTestVendorId2, ECapabilityProtServ, ECapabilityWriteDeviceData);
  1347 		//Current process is missing vid and ECapabilityWriteDeviceData
  1348 		test(failResult!=!(vid.CheckPolicy(RProcess())));
  1349 		test(failResult!=!(vid.CheckPolicy(RThread())));
  1350 		TPtrC8 ptr = vid.Package();
  1351 		TInt r = Session.Send(CTestSession::ETestSecurityPolicyAgainstMessage,TIpcArgs(&ptr));
  1352 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1353 		r = Session.Send(CTestSession::ETestSecurityPolicyAgainstCreator,TIpcArgs(&ptr));
  1354 		test(r==failResult?CTestSession::EPolicyCheckFailed:CTestSession::EPolicyCheckPassed);
  1355 		}
  1356 
  1357 	test.Next(_L("Macros for compile-time construction"));
  1358 		{
  1359 		static _LIT_SECURITY_POLICY_C7(pc7,1,2,3,4,5,6,7);
  1360 		static _LIT_SECURITY_POLICY_C6(pc6,1,2,3,4,5,6);
  1361 		static _LIT_SECURITY_POLICY_C5(pc5,1,2,3,4,5);
  1362 		static _LIT_SECURITY_POLICY_C4(pc4,1,2,3,4);
  1363 		static _LIT_SECURITY_POLICY_C3(pc3,1,2,3);
  1364 		static _LIT_SECURITY_POLICY_C2(pc2,1,2);
  1365 		static _LIT_SECURITY_POLICY_C1(pc1,1);
  1366 		test(SecurityPoliciesEqual(pc7,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4,(TCapability)5,(TCapability)6,(TCapability)7)));
  1367 		test(SecurityPoliciesEqual(pc6,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4,(TCapability)5,(TCapability)6)));
  1368 		test(SecurityPoliciesEqual(pc5,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4,(TCapability)5)));
  1369 		test(SecurityPoliciesEqual(pc4,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4)));
  1370 		test(SecurityPoliciesEqual(pc3,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3)));
  1371 		test(SecurityPoliciesEqual(pc2,TSecurityPolicy((TCapability)1,(TCapability)2)));
  1372 		test(SecurityPoliciesEqual(pc1,TSecurityPolicy((TCapability)1)));
  1373 		
  1374 		static _LIT_SECURITY_POLICY_S3(ps3,0x12345678,1,2,3);
  1375 		static _LIT_SECURITY_POLICY_S2(ps2,0x12345678,1,2);
  1376 		static _LIT_SECURITY_POLICY_S1(ps1,0x12345678,1);
  1377 		static _LIT_SECURITY_POLICY_S0(ps0,0x12345678);
  1378 		test(SecurityPoliciesEqual(ps3,TSecurityPolicy(TSecureId(0x12345678),(TCapability)1,(TCapability)2,(TCapability)3)));
  1379 		test(SecurityPoliciesEqual(ps2,TSecurityPolicy(TSecureId(0x12345678),(TCapability)1,(TCapability)2)));
  1380 		test(SecurityPoliciesEqual(ps1,TSecurityPolicy(TSecureId(0x12345678),(TCapability)1)));
  1381 		test(SecurityPoliciesEqual(ps0,TSecurityPolicy(TSecureId(0x12345678))));
  1382 
  1383 		static _LIT_SECURITY_POLICY_V3(pv3,0x12345678,1,2,3);
  1384 		static _LIT_SECURITY_POLICY_V2(pv2,0x12345678,1,2);
  1385 		static _LIT_SECURITY_POLICY_V1(pv1,0x12345678,1);
  1386 		static _LIT_SECURITY_POLICY_V0(pv0,0x12345678);
  1387 		test(SecurityPoliciesEqual(pv3,TSecurityPolicy(TVendorId(0x12345678),(TCapability)1,(TCapability)2,(TCapability)3)));
  1388 		test(SecurityPoliciesEqual(pv2,TSecurityPolicy(TVendorId(0x12345678),(TCapability)1,(TCapability)2)));
  1389 		test(SecurityPoliciesEqual(pv1,TSecurityPolicy(TVendorId(0x12345678),(TCapability)1)));
  1390 		test(SecurityPoliciesEqual(pv0,TSecurityPolicy(TVendorId(0x12345678))));
  1391 
  1392 		static _LIT_SECURITY_POLICY_FAIL(fail);
  1393 		static _LIT_SECURITY_POLICY_PASS(pass);
  1394 		test(SecurityPoliciesEqual(fail,TSecurityPolicy(TSecurityPolicy::EAlwaysFail)));
  1395 		test(SecurityPoliciesEqual(pass,TSecurityPolicy(TSecurityPolicy::EAlwaysPass)));
  1396 		}
  1397 
  1398 	test.Next(_L("Macros for compile-time initialisation"));
  1399 		{
  1400 		const TStaticSecurityPolicy pc7 = _INIT_SECURITY_POLICY_C7(1,2,3,4,5,6,7);
  1401 		const TStaticSecurityPolicy pc6 = _INIT_SECURITY_POLICY_C6(1,2,3,4,5,6);
  1402 		const TStaticSecurityPolicy pc5 = _INIT_SECURITY_POLICY_C5(1,2,3,4,5);
  1403 		const TStaticSecurityPolicy pc4 = _INIT_SECURITY_POLICY_C4(1,2,3,4);
  1404 		const TStaticSecurityPolicy pc3 = _INIT_SECURITY_POLICY_C3(1,2,3);
  1405 		const TStaticSecurityPolicy pc2 = _INIT_SECURITY_POLICY_C2(1,2);
  1406 		const TStaticSecurityPolicy pc1 = _INIT_SECURITY_POLICY_C1(1);
  1407 		test(SecurityPoliciesEqual(pc7,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4,(TCapability)5,(TCapability)6,(TCapability)7)));
  1408 		test(SecurityPoliciesEqual(pc6,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4,(TCapability)5,(TCapability)6)));
  1409 		test(SecurityPoliciesEqual(pc5,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4,(TCapability)5)));
  1410 		test(SecurityPoliciesEqual(pc4,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3,(TCapability)4)));
  1411 		test(SecurityPoliciesEqual(pc3,TSecurityPolicy((TCapability)1,(TCapability)2,(TCapability)3)));
  1412 		test(SecurityPoliciesEqual(pc2,TSecurityPolicy((TCapability)1,(TCapability)2)));
  1413 		test(SecurityPoliciesEqual(pc1,TSecurityPolicy((TCapability)1)));
  1414 		
  1415 		const TStaticSecurityPolicy ps3 = _INIT_SECURITY_POLICY_S3(0x12345678,1,2,3);
  1416 		const TStaticSecurityPolicy ps2 = _INIT_SECURITY_POLICY_S2(0x12345678,1,2);
  1417 		const TStaticSecurityPolicy ps1 = _INIT_SECURITY_POLICY_S1(0x12345678,1);
  1418 		const TStaticSecurityPolicy ps0 = _INIT_SECURITY_POLICY_S0(0x12345678);
  1419 		test(SecurityPoliciesEqual(ps3,TSecurityPolicy(TSecureId(0x12345678),(TCapability)1,(TCapability)2,(TCapability)3)));
  1420 		test(SecurityPoliciesEqual(ps2,TSecurityPolicy(TSecureId(0x12345678),(TCapability)1,(TCapability)2)));
  1421 		test(SecurityPoliciesEqual(ps1,TSecurityPolicy(TSecureId(0x12345678),(TCapability)1)));
  1422 		test(SecurityPoliciesEqual(ps0,TSecurityPolicy(TSecureId(0x12345678))));
  1423 
  1424 		const TStaticSecurityPolicy pv3 = _INIT_SECURITY_POLICY_V3(0x12345678,1,2,3);
  1425 		const TStaticSecurityPolicy pv2 = _INIT_SECURITY_POLICY_V2(0x12345678,1,2);
  1426 		const TStaticSecurityPolicy pv1 = _INIT_SECURITY_POLICY_V1(0x12345678,1);
  1427 		const TStaticSecurityPolicy pv0 = _INIT_SECURITY_POLICY_V0(0x12345678);
  1428 		test(SecurityPoliciesEqual(pv3,TSecurityPolicy(TVendorId(0x12345678),(TCapability)1,(TCapability)2,(TCapability)3)));
  1429 		test(SecurityPoliciesEqual(pv2,TSecurityPolicy(TVendorId(0x12345678),(TCapability)1,(TCapability)2)));
  1430 		test(SecurityPoliciesEqual(pv1,TSecurityPolicy(TVendorId(0x12345678),(TCapability)1)));
  1431 		test(SecurityPoliciesEqual(pv0,TSecurityPolicy(TVendorId(0x12345678))));
  1432 
  1433 		const TStaticSecurityPolicy fail = _INIT_SECURITY_POLICY_FAIL;
  1434 		const TStaticSecurityPolicy pass = _INIT_SECURITY_POLICY_PASS;
  1435 		test(SecurityPoliciesEqual(fail,TSecurityPolicy(TSecurityPolicy::EAlwaysFail)));
  1436 		test(SecurityPoliciesEqual(pass,TSecurityPolicy(TSecurityPolicy::EAlwaysPass)));
  1437 
  1438 		}
  1439 
  1440 	test.End();
  1441 	}
  1442 
  1443 #define CHECK_NAME(name)	\
  1444 	test(0==TPtrC8((TUint8*)#name).Compare(TPtrC8((TUint8*)CapabilityNames[ECapability##name])));
  1445 
  1446 void TestCapabilityNames()
  1447 	{
  1448 	CHECK_NAME(TCB);
  1449 	CHECK_NAME(CommDD);
  1450 	CHECK_NAME(PowerMgmt);
  1451 	CHECK_NAME(MultimediaDD);
  1452 	CHECK_NAME(ReadDeviceData);
  1453 	CHECK_NAME(WriteDeviceData);
  1454 	CHECK_NAME(DRM);
  1455 	CHECK_NAME(TrustedUI);
  1456 	CHECK_NAME(ProtServ);
  1457 	CHECK_NAME(DiskAdmin);
  1458 	CHECK_NAME(NetworkControl);
  1459 	CHECK_NAME(AllFiles);
  1460 	CHECK_NAME(SwEvent);
  1461 	CHECK_NAME(NetworkServices);
  1462 	CHECK_NAME(LocalServices);
  1463 	CHECK_NAME(ReadUserData);
  1464 	CHECK_NAME(WriteUserData);
  1465 	CHECK_NAME(Location);
  1466 	CHECK_NAME(SurroundingsDD);
  1467 	CHECK_NAME(UserEnvironment);
  1468 	}
  1469 
  1470 
  1471 void TestKernelAPIs()
  1472 	{
  1473 	RLddTest ldd;
  1474 	TInt r=User::LoadLogicalDevice(_L("D_SLDD.LDD"));
  1475 	test(r==KErrNone || r==KErrAlreadyExists);
  1476 	r=ldd.OpenLocal();
  1477 	test(r==KErrNone);
  1478 
  1479 	RLddTest::TIds ids;
  1480 	memclr(&ids,sizeof(ids));
  1481 	ldd.GetIds(ids);
  1482 	test.Printf(_L("Thread VID,SID = %08x,%08x\n\r"),ids.iThreadVID.iId,ids.iThreadSID.iId);
  1483 	test.Printf(_L("Process VID,SID = %08x,%08x\n\r"),ids.iProcessVID.iId,ids.iProcessSID.iId);
  1484 	test(ids.iThreadVID==KTestVendorId);
  1485 	test(ids.iThreadSID==KTestSecureId);
  1486 	test(ids.iProcessVID==KTestVendorId);
  1487 	test(ids.iProcessSID==KTestSecureId);
  1488 
  1489 	// Test kernel-mode TSecurityInfo-getting APIs
  1490 	TSecurityInfo infoProcess, infoThread;
  1491 	ldd.GetSecureInfos(&infoThread, &infoProcess);
  1492 	// Check the vendor & secure IDs are what's expected
  1493 	test(infoThread.iVendorId==KTestVendorId);
  1494 	test(infoThread.iSecureId==KTestSecureId);
  1495 	test(infoProcess.iVendorId==KTestVendorId);
  1496 	test(infoProcess.iSecureId==KTestSecureId);
  1497 	// Check process caps == thread caps
  1498 	TUint32* capsT = (TUint32*)&infoThread.iCaps;
  1499 	TUint32* capsP = (TUint32*)&infoProcess.iCaps;
  1500 	test(capsT[0]==capsP[0]);
  1501 	test(capsT[1]==capsP[1]);
  1502 	// Check the caps match what the user API gives
  1503 	RProcess this_process;
  1504 	for (TInt i=0 ; i<64 ; i++) {
  1505 		TCapability cap = (TCapability)i;
  1506 		test(infoProcess.iCaps.HasCapability(cap) == this_process.HasCapability(cap));
  1507 	}
  1508 
  1509 
  1510 	ldd.Close();
  1511 	}
  1512 
  1513 
  1514 void TestPlatSecUnlocked()
  1515 	{
  1516 	RLddTest ldd;
  1517 	TInt r = User::LoadLogicalDevice(_L("D_SLDD.LDD"));
  1518 	test(r == KErrNone || r == KErrAlreadyExists);
  1519 	r = ldd.OpenLocal();
  1520 	test_KErrNone(r);
  1521 
  1522 	TUint32 flags0, flags;
  1523 	TInt enforced;
  1524 
  1525 	flags0 = ldd.GetKernelConfigFlags();
  1526 
  1527 	const TUint32 bits = (TUint32)(EKernelConfigTest | EKernelConfigPlatSecEnforcement);
  1528 
  1529 	// Different test cases depending on whether __PLATSEC_UNLOCKED__ defined.
  1530 	// Ask the kernel whether EKernelConfigPlatSecLocked is set, and hope that
  1531 	// it's not lying to us!
  1532 	//
  1533 	// Best thing to do is to check the log and verify the printf() output.
  1534 	//
  1535 
  1536 	if (PlatSec::ConfigSetting(PlatSec::EPlatSecLocked))
  1537 		{
  1538 		/*
  1539 		 * Tests for __PLATSEC_UNLOCKED__ not defined
  1540 		 *
  1541 		 * The device driver is built with __PLATSEC_FORCED_FLAGS__ set to 0, so we can't use
  1542 		 * its GetKernelConfigFlags() interface (the accessor functions are inlines) when we're
  1543 		 * testing for PlatSec locked.  Instead, use PlatSec::ConfigSetting(), which makes an
  1544 		 * exec call to get iKernelConfigFlags.
  1545 		 */
  1546 		test.Printf(_L("__PLATSEC_UNLOCKED_ does NOT appear to have been used\n"));
  1547 
  1548 		/* Check that PlatSecEnforcement is set */
  1549 		enforced = PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement);
  1550 		test(enforced != 0);
  1551 
  1552 		/* Check that the device driver is able to set our test bit */
  1553 		ldd.SetKernelConfigFlags(flags0 | EKernelConfigTest);
  1554 
  1555 		flags = ldd.GetKernelConfigFlags();
  1556 		test((flags & EKernelConfigTest) == EKernelConfigTest);
  1557 
  1558 		/* Check that the device driver is able to clear our test bit, but not able to clear enforcement bit */
  1559 		ldd.SetKernelConfigFlags(flags0 & ~bits);
  1560 
  1561 		enforced = PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement);
  1562 		test(enforced != 0);
  1563 		}
  1564 	else
  1565 		{
  1566 		/*
  1567 		 * Tests for __PLATSEC_UNLOCKED__ defined
  1568 		 */
  1569 		test.Printf(_L("__PLATSEC_UNLOCKED_ DOES appear to have been used\n"));
  1570 
  1571 		/* Check that the device driver is able to set our test bit and PlatSecEnforcement */
  1572 		ldd.SetKernelConfigFlags(flags0 | bits);
  1573 
  1574 		flags = ldd.GetKernelConfigFlags();
  1575 		test((flags & bits) == bits);
  1576 
  1577 		/* And verify that the kernel sees the same result */
  1578 		enforced = PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement);
  1579 		test(enforced != 0);			// (yes, "!= 0" is redundant)
  1580 
  1581 		/* Check that the device driver is able to clear our test bit and PlatSecEnforcement */
  1582 		ldd.SetKernelConfigFlags(flags0 & ~bits);
  1583 
  1584 		flags = ldd.GetKernelConfigFlags();
  1585 		test((flags & bits) == 0);
  1586 
  1587 		/* Verify that the kernel sees the same result */
  1588 		enforced = PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement);
  1589 		test(enforced == 0);
  1590 		}
  1591 
  1592 	/* Restore original flags value */
  1593 	ldd.SetKernelConfigFlags(flags0);
  1594 
  1595 	// Now test handling of iDisabledCapabilities
  1596 
  1597 	const TUint32 test_value = 0x31415926;						// Just some random number
  1598 
  1599 	TCapabilitySet disabled0, disabled;
  1600 	disabled0.SetDisabled();
  1601 
  1602 	SCapabilitySet *caps0 = (SCapabilitySet *)&disabled0;
  1603 	SCapabilitySet *caps = (SCapabilitySet *)&disabled;
  1604 
  1605 	if (PlatSec::ConfigSetting(PlatSec::EPlatSecLocked))
  1606 		{
  1607 		/*
  1608 		 * Tests for __PLATSEC_UNLOCKED__ not defined
  1609 		 */
  1610 
  1611 		/* Check that the capability set is 0 */
  1612 		int i;
  1613 
  1614 		for (i = 0; i < SCapabilitySet::ENCapW; i++)
  1615 			{
  1616 			test_Equal(0, caps0->iCaps[i]);
  1617 			}
  1618 
  1619 		/* Check that the device driver is not able to set word 0 of disabled capabilities */
  1620 		ldd.SetDisabledCapabilities0(test_value);
  1621 
  1622 		/*
  1623 		 * It's okay to use SetDisabled() here, since this makes an exec call to get the
  1624 		 * set of disabled capabilities.
  1625 		 */
  1626 		disabled.SetDisabled();
  1627 		test_Equal(0, caps->iCaps[0]);
  1628 		}
  1629 	else
  1630 		{
  1631 		/*
  1632 		 * Tests for __PLATSEC_UNLOCKED__ defined
  1633 		 */
  1634 
  1635 		/* Check that the device driver is able to set word 0 of disabled capabilities */
  1636 		ldd.SetDisabledCapabilities0(test_value);
  1637 
  1638 		disabled.SetDisabled();
  1639 		test_Equal(test_value, caps->iCaps[0]);
  1640 		}
  1641 
  1642 	/* Restore original value */
  1643 	ldd.SetDisabledCapabilities0(caps0->iCaps[0]);
  1644 
  1645 	ldd.Close();
  1646 	}
  1647 
  1648 #include <e32svr.h>
  1649 
  1650 GLDEF_C TInt E32Main()
  1651     {
  1652 	PlatSecEnforcement = PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement);
  1653 
  1654 	TBuf16<512> cmd;
  1655 	User::CommandLine(cmd);
  1656 	if(cmd.Length() && TChar(cmd[0]).IsDigit())
  1657 		{
  1658 		TInt function = -1;
  1659 		TInt arg1 = -1;
  1660 		TInt arg2 = -1;
  1661 		TLex lex(cmd);
  1662 
  1663 		lex.Val(function);
  1664 		lex.SkipSpace();
  1665 		lex.Val(arg1);
  1666 		lex.SkipSpace();
  1667 		lex.Val(arg2);
  1668 		return DoTestProcess(function,arg1,arg2);
  1669 		}
  1670 
  1671 	test.Title();
  1672 
  1673 	if(PlatSecEnforcement)
  1674 		test.Printf(_L("PlatSecConfig appears to be ON\n"));
  1675 	else
  1676 		test.Printf(_L("PlatSecConfig appears to be OFF\n"));
  1677 
  1678 	test.Start(_L("Starting test server"));
  1679 	RTestProcess server;
  1680 	TRequestStatus rendezvous;
  1681 	server.Create(~KTestCapabilities,ETestProcessServer,0,0);
  1682 	server.Rendezvous(rendezvous);
  1683 	server.Resume();
  1684 	User::WaitForRequest(rendezvous);
  1685 	test(rendezvous==KServerRendezvous);
  1686 
  1687 	test.Next(_L("Openning server session"));
  1688 	TInt r = Session.Connect();
  1689 	RDebug::Print(_L("%d"),r);
  1690 	test(r==KErrNone);
  1691 
  1692 	test.Next(_L("Test Capability Names"));
  1693 	TestCapabilityNames();
  1694 
  1695 	test.Next(_L("Test TCapabilitySet"));
  1696 	TestCapabilitySet();
  1697 
  1698 	test.Next(_L("Test TSecurityInfo"));
  1699 	TestSecurityInfo();
  1700 
  1701 	test.Next(_L("Test SecureId functions"));
  1702 	TestSecureId();
  1703 
  1704 	test.Next(_L("Test VendorId functions"));
  1705 	TestVendorId();
  1706 
  1707 	test.Next(_L("Test HasCapability functions"));
  1708 	TestHasCapability();
  1709 
  1710 	test.Next(_L("Test TSecurityPolicy"));
  1711 	TestSecurityPolicy();
  1712 
  1713 	test.Next(_L("Test Kernel APIs"));
  1714 	TestKernelAPIs();
  1715 
  1716 	test.Next(_L("Test __PLATSEC_UNLOCKED__"));
  1717 	TestPlatSecUnlocked();
  1718 
  1719 	test.Next(_L("Test diagnostic message suppression"));
  1720 	RThread().HasCapability(ECapabilityReadUserData,0);
  1721 	test.Printf(_L("There should be a diagnostic message just before this\n"));
  1722 	RThread().HasCapability(ECapabilityReadUserData,__PLATSEC_DIAGNOSTIC_STRING("You should see this"));
  1723 	test.Printf(_L("There should be a diagnostic message just before this\n"));
  1724 	RThread().HasCapability(ECapabilityReadUserData,KSuppressPlatSecDiagnostic);
  1725 	test.Printf(_L("There should NOT be a diagnostic message just before this\n"));
  1726 
  1727 	test.Next(_L("Closing server session"));
  1728 	Session.Send(CTestSession::EShutdown);
  1729 	Session.Close();
  1730 	server.Close();
  1731 
  1732 	test.End();
  1733 	return(0);
  1734     }
  1735