os/kernelhwsrv/kerneltest/e32test/secure/t_polsvr.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/secure/t_polsvr.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1898 @@
     1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\secure\t_polsvr.cpp
    1.18 +// Overview:
    1.19 +// Policy Server Tests
    1.20 +// API Information:
    1.21 +// CPolicyServer
    1.22 +// Details:
    1.23 +// - Perform various policy server tests:
    1.24 +// Server1 has implementations of CustomSecurityCheckL and
    1.25 +// CustomFailureActionL This is the test for all the paths in a policy servers
    1.26 +// implementations -- not all connect paths though
    1.27 +// This test also ensures that every path through the binary search is
    1.28 +// covered.
    1.29 +// Policy2,3,4,5,6, are bad policies that should cause the server to panic
    1.30 +// in debug mode.  In release, they'll just pass, however, if you later tried
    1.31 +// to use them, something would go horribly wrong
    1.32 +// Policies 7,8,9 check various types of connect policies.
    1.33 +// Server 1,2,3 are used here all with policy 8 because you can only test 1
    1.34 +// type of connect policy per server.
    1.35 +// Sever2 does not have implementations of CustomSecurityCheckL and
    1.36 +// CustomFailureActionL. When these functions are called it should crash
    1.37 +// Server4 is used for checking what happens when the custom functions use
    1.38 +// another active object.  This test encompasses leaving due to OOM, and
    1.39 +// cancellation.
    1.40 +// Platforms/Drives/Compatibility:
    1.41 +// All.
    1.42 +// Assumptions/Requirement/Pre-requisites:
    1.43 +// Failures and causes:
    1.44 +// Base Port information:
    1.45 +// 
    1.46 +//
    1.47 +
    1.48 +#include <e32test.h>
    1.49 +#include <e32debug.h>
    1.50 +
    1.51 +LOCAL_D RTest test(_L("T_POLSVR"));
    1.52 +
    1.53 +_LIT(KPolSvr, "T_POLSVR");
    1.54 +
    1.55 +#define Debug(x) RDebug::Print(_L(x))
    1.56 +
    1.57 +const TUint32 KTestCapabilities =(1<<ECapabilityTCB)
    1.58 +								|(1<<ECapabilityPowerMgmt)
    1.59 +								|(1<<ECapabilityReadDeviceData)
    1.60 +								|(1<<ECapabilityDRM)
    1.61 +								|(1<<ECapabilityProtServ)
    1.62 +								|(1<<ECapabilityNetworkControl)
    1.63 +								|(1<<ECapabilitySwEvent)
    1.64 +								|(1<<ECapabilityLocalServices)
    1.65 +								|(1<<ECapabilityWriteUserData);
    1.66 +
    1.67 +const TInt KCustomCheckMask		= 0x01000000;
    1.68 +const TInt KCustomActionMask	= 0x02000000;
    1.69 +const TInt KServiceLMask		= 0x04000000;
    1.70 +const TInt KActiveCheckMask 	= 0x08000000;
    1.71 +const TInt KActiveActionMask	= 0x00100000;
    1.72 +const TInt KActiveRunLMask  	= 0x00200000;
    1.73 +const TInt KActiveRunErrorMask	= 0x00400000;
    1.74 +const TInt KActiveDoCancelMask	= 0x00800000;
    1.75 +const TInt KServiceErrorMask	= 0x00010000;
    1.76 +
    1.77 +//Ideally this constant should go in your derived server.  However, I couldn't
    1.78 +//be bothered to put the same constant in every one of the derived servers in
    1.79 +//this test code
    1.80 +const TInt KQueryUser = -1;
    1.81 +
    1.82 +void OrInFlags(const RMessage2& aMsg, TUint aMask)
    1.83 +	{
    1.84 +	TBuf8<4> flags(0);
    1.85 +	TInt r = aMsg.Read(0, flags);
    1.86 +	test(r == KErrNone);
    1.87 +	(*(TUint32*)(flags.Ptr())) |= aMask;
    1.88 +	flags.SetLength(4);
    1.89 +	r = aMsg.Write(0, flags);
    1.90 +	test(r == KErrNone);
    1.91 +	}
    1.92 +
    1.93 +void SetFlags(TDes8& aDes, TUint aValue)
    1.94 +	{
    1.95 +	(*(TUint32*)(aDes.Ptr())) = aValue;
    1.96 +	}
    1.97 +
    1.98 +TUint FlagsValue(const TDes8& aDes)
    1.99 +	{
   1.100 +	return (*(TUint32*)(aDes.Ptr()));
   1.101 +	}
   1.102 +
   1.103 +enum TTestProcessFunctions
   1.104 +	{
   1.105 +	ETestProcessPolicyServer,
   1.106 +	};
   1.107 +
   1.108 +enum TTestServerIndex
   1.109 +	{
   1.110 +	ETestServer1=0,
   1.111 +	ETestServer2,
   1.112 +	ETestServer3,
   1.113 +	ETestServer4,
   1.114 +	};
   1.115 +
   1.116 +enum TServerPolicyIndex
   1.117 +	{
   1.118 +	EPolicy1=0,
   1.119 +	EPolicy2,
   1.120 +	EPolicy3,
   1.121 +	EPolicy4,
   1.122 +	EPolicy5,
   1.123 +	EPolicy6,
   1.124 +	EPolicy7,
   1.125 +	EPolicy8,
   1.126 +	EPolicy9,
   1.127 +	EPolicy10,
   1.128 +	EPolicy11,
   1.129 +	EPolicy12,
   1.130 +	};
   1.131 +
   1.132 +//
   1.133 +// EPolicy1
   1.134 +//
   1.135 +
   1.136 +const TUint gServerPolicy1RangeCount = 12;
   1.137 +const TInt gServerPolicy1Ranges[gServerPolicy1RangeCount] = { 
   1.138 +	0, //ENotSupported
   1.139 +	1, //ECustomCheck
   1.140 +	6, //EAlwaysPass
   1.141 +	7, //ENotSupported
   1.142 +	8, //->0
   1.143 +	9, //->1
   1.144 +	10, //->2
   1.145 +	11, //->3
   1.146 +	12, //->3
   1.147 +	13, //ENotSupported
   1.148 +	100, //EAlwaysPass -> Shutdown
   1.149 +	101, //ENotSupported
   1.150 +	};
   1.151 +
   1.152 +const TUint8 gServerPolicy1ElementsIndex[] = { 
   1.153 +	CPolicyServer::ENotSupported,
   1.154 +	CPolicyServer::ECustomCheck, 
   1.155 +	CPolicyServer::EAlwaysPass, 
   1.156 +	CPolicyServer::ENotSupported,
   1.157 +	0, //RequireNetworkControl or EFail
   1.158 +	1, //RequireNetworkControl or EQueryUser
   1.159 +	2, //RequireDiskAdmin or EFail
   1.160 +	3, //RequireDiskAdmin or EQueryUser
   1.161 +	3, //RequireDiskAdmin or EQueryUser
   1.162 +	CPolicyServer::ENotSupported,
   1.163 +	CPolicyServer::EAlwaysPass, 
   1.164 +	CPolicyServer::ENotSupported,
   1.165 +	};
   1.166 +
   1.167 +const CPolicyServer::TPolicyElement gServerPolicy1Elements[] =
   1.168 +	{
   1.169 +	{_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl),CPolicyServer::EFailClient},
   1.170 +	{_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl),KQueryUser},
   1.171 +	{_INIT_SECURITY_POLICY_C1(ECapabilityDiskAdmin),CPolicyServer::EFailClient},
   1.172 +	{_INIT_SECURITY_POLICY_C1(ECapabilityDiskAdmin),KQueryUser},
   1.173 +	};
   1.174 +
   1.175 +const CPolicyServer::TPolicy gServerPolicy1 = 
   1.176 +	{
   1.177 +	CPolicyServer::EAlwaysPass,gServerPolicy1RangeCount,
   1.178 +	gServerPolicy1Ranges,
   1.179 +	gServerPolicy1ElementsIndex,
   1.180 +	gServerPolicy1Elements,
   1.181 +	};
   1.182 +
   1.183 +//
   1.184 +//EPolicy2
   1.185 +//
   1.186 +
   1.187 +const TUint gServerPolicy2RangeCount = 1;
   1.188 +//Invalid Policy -- doesn't start with 0
   1.189 +const TInt gServerPolicy2Ranges[gServerPolicy2RangeCount] = { 
   1.190 +	1, //KErrNotSupported
   1.191 +	};
   1.192 +
   1.193 +const TUint8 gServerPolicy2ElementsIndex[] = { 
   1.194 +	CPolicyServer::ENotSupported,
   1.195 +	};
   1.196 +
   1.197 +const CPolicyServer::TPolicyElement gServerPolicy2Elements[] =
   1.198 +	{
   1.199 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EFailClient},
   1.200 +	};
   1.201 +
   1.202 +const CPolicyServer::TPolicy gServerPolicy2 = 
   1.203 +	{
   1.204 +	0,gServerPolicy2RangeCount,
   1.205 +	gServerPolicy2Ranges,
   1.206 +	gServerPolicy2ElementsIndex,
   1.207 +	gServerPolicy2Elements
   1.208 +	};
   1.209 +
   1.210 +//
   1.211 +//EPolicy3
   1.212 +//
   1.213 +
   1.214 +const TUint gServerPolicy3RangeCount = 12;
   1.215 +//Invalid Policy -- range values not increasing
   1.216 +const TInt gServerPolicy3Ranges[gServerPolicy3RangeCount] = { 
   1.217 +	0, //ECustomCheck
   1.218 +	6, //EAlwaysPass
   1.219 +	7, //ENotSupported
   1.220 +	8, //->0
   1.221 +	9, //->1
   1.222 +	10, //->2
   1.223 +	11, //->3
   1.224 +	12, //->3
   1.225 +	13, //ENotSupported
   1.226 +	100, //EAlwaysPass -> Shutdown
   1.227 +	101, //ENotSupported
   1.228 +	99, //EAlwaysPass
   1.229 +	};
   1.230 +
   1.231 +const TUint8 gServerPolicy3ElementsIndex[] = { 
   1.232 +	CPolicyServer::ECustomCheck, 
   1.233 +	CPolicyServer::EAlwaysPass, 
   1.234 +	CPolicyServer::ENotSupported,
   1.235 +	0, //RequireNetworkControl or EFail
   1.236 +	1, //RequireNetworkControl or EQueryUser
   1.237 +	2, //RequireDiskAdmin or EFail
   1.238 +	3, //RequireDiskAdmin or EQueryUser
   1.239 +	3, //RequireDiskAdmin or EQueryUser
   1.240 +	CPolicyServer::ENotSupported,
   1.241 +	CPolicyServer::EAlwaysPass, 
   1.242 +	CPolicyServer::ENotSupported,
   1.243 +	CPolicyServer::EAlwaysPass, 
   1.244 +	};
   1.245 +
   1.246 +const CPolicyServer::TPolicyElement gServerPolicy3Elements[] =
   1.247 +	{
   1.248 +	{_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl),CPolicyServer::EFailClient},
   1.249 +	{_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl),KQueryUser},
   1.250 +	{_INIT_SECURITY_POLICY_C1(ECapabilityDiskAdmin),CPolicyServer::EFailClient},
   1.251 +	{_INIT_SECURITY_POLICY_C1(ECapabilityDiskAdmin),KQueryUser},
   1.252 +	};
   1.253 +
   1.254 +const CPolicyServer::TPolicy gServerPolicy3 = 
   1.255 +	{
   1.256 +	0,gServerPolicy3RangeCount,
   1.257 +	gServerPolicy3Ranges,
   1.258 +	gServerPolicy3ElementsIndex,
   1.259 +	gServerPolicy3Elements
   1.260 +	};
   1.261 +
   1.262 +//
   1.263 +//EPolicy4
   1.264 +//
   1.265 +
   1.266 +const TUint gServerPolicy4RangeCount = 1;
   1.267 +//Invalid Policy -- Elements Index has invalid values
   1.268 +const TInt gServerPolicy4Ranges[gServerPolicy4RangeCount] = { 
   1.269 +	0, //Invalid value
   1.270 +	};
   1.271 +
   1.272 +const TUint8 gServerPolicy4ElementsIndex[] = { 
   1.273 +	CPolicyServer::ESpecialCaseHardLimit,
   1.274 +	};
   1.275 +
   1.276 +const CPolicyServer::TPolicyElement gServerPolicy4Elements[] =
   1.277 +	{
   1.278 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EFailClient},
   1.279 +	};
   1.280 +
   1.281 +const CPolicyServer::TPolicy gServerPolicy4 = 
   1.282 +	{
   1.283 +	0,gServerPolicy4RangeCount,
   1.284 +	gServerPolicy4Ranges,
   1.285 +	gServerPolicy4ElementsIndex,
   1.286 +	gServerPolicy4Elements
   1.287 +	};
   1.288 +
   1.289 +//
   1.290 +//EPolicy5
   1.291 +//
   1.292 +
   1.293 +const TUint gServerPolicy5RangeCount = 1;
   1.294 +//Invalid Policy -- Elements Index has invalid values
   1.295 +const TInt gServerPolicy5Ranges[gServerPolicy5RangeCount] = { 
   1.296 +	0, //uses Invalid value
   1.297 +	};
   1.298 +
   1.299 +const TUint8 gServerPolicy5ElementsIndex[] = { 
   1.300 +	//Uses invalid value 
   1.301 +	CPolicyServer::ESpecialCaseLimit,
   1.302 +	};
   1.303 +
   1.304 +const CPolicyServer::TPolicyElement gServerPolicy5Elements[] =
   1.305 +	{
   1.306 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EFailClient},
   1.307 +	};
   1.308 +
   1.309 +const CPolicyServer::TPolicy gServerPolicy5 = 
   1.310 +	{
   1.311 +	0,gServerPolicy5RangeCount,
   1.312 +	gServerPolicy5Ranges,
   1.313 +	gServerPolicy5ElementsIndex,
   1.314 +	gServerPolicy5Elements
   1.315 +	};
   1.316 +
   1.317 +//
   1.318 +//EPolicy6
   1.319 +//
   1.320 +
   1.321 +const TUint gServerPolicy6RangeCount = 1;
   1.322 +//Invalid Policy -- Elements Index has invalid values
   1.323 +const TInt gServerPolicy6Ranges[gServerPolicy6RangeCount] = { 
   1.324 +	0,
   1.325 +	};
   1.326 +
   1.327 +const TUint8 gServerPolicy6ElementsIndex[] = { 
   1.328 +	CPolicyServer::ENotSupported,
   1.329 +	};
   1.330 +
   1.331 +const CPolicyServer::TPolicyElement gServerPolicy6Elements[] =
   1.332 +	{
   1.333 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EFailClient},
   1.334 +	};
   1.335 +
   1.336 +const CPolicyServer::TPolicy gServerPolicy6 = 
   1.337 +	{
   1.338 +	//Uses invalid value for iOnConnect
   1.339 +	CPolicyServer::ESpecialCaseHardLimit,gServerPolicy6RangeCount,
   1.340 +	gServerPolicy6Ranges,
   1.341 +	gServerPolicy6ElementsIndex,
   1.342 +	gServerPolicy6Elements
   1.343 +	};
   1.344 +
   1.345 +//
   1.346 +//EPolicy7
   1.347 +//
   1.348 +// Connect not supported
   1.349 +const TUint gServerPolicy7RangeCount = 1;
   1.350 +const TInt gServerPolicy7Ranges[gServerPolicy7RangeCount] = { 
   1.351 +	0,
   1.352 +	};
   1.353 +
   1.354 +const TUint8 gServerPolicy7ElementsIndex[] = { 
   1.355 +	0
   1.356 +	};
   1.357 +
   1.358 +const CPolicyServer::TPolicyElement gServerPolicy7Elements[] =
   1.359 +	{
   1.360 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EFailClient},
   1.361 +	};
   1.362 +
   1.363 +const CPolicyServer::TPolicy gServerPolicy7 = 
   1.364 +	{
   1.365 +	CPolicyServer::ENotSupported,gServerPolicy7RangeCount,
   1.366 +	gServerPolicy7Ranges,
   1.367 +	gServerPolicy7ElementsIndex,
   1.368 +	gServerPolicy7Elements
   1.369 +	};
   1.370 +
   1.371 +//
   1.372 +//EPolicy8
   1.373 +//
   1.374 +// Connect Custom Check
   1.375 +const TUint gServerPolicy8RangeCount = 1;
   1.376 +const TInt gServerPolicy8Ranges[gServerPolicy8RangeCount] = { 
   1.377 +	0,
   1.378 +	};
   1.379 +
   1.380 +const TUint8 gServerPolicy8ElementsIndex[] = { 
   1.381 +	0
   1.382 +	};
   1.383 +
   1.384 +const CPolicyServer::TPolicyElement gServerPolicy8Elements[] =
   1.385 +	{
   1.386 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EPanicClient},
   1.387 +	};
   1.388 +
   1.389 +const CPolicyServer::TPolicy gServerPolicy8 = 
   1.390 +	{
   1.391 +	CPolicyServer::ECustomCheck,gServerPolicy8RangeCount,
   1.392 +	gServerPolicy8Ranges,
   1.393 +	gServerPolicy8ElementsIndex,
   1.394 +	gServerPolicy8Elements
   1.395 +	};
   1.396 +
   1.397 +//
   1.398 +//EPolicy9
   1.399 +//
   1.400 +// Connect has a static policy but it fails.
   1.401 +const TUint gServerPolicy9RangeCount = 1;
   1.402 +const TInt gServerPolicy9Ranges[gServerPolicy9RangeCount] = { 
   1.403 +	0,
   1.404 +	};
   1.405 +
   1.406 +const TUint8 gServerPolicy9ElementsIndex[] = { 
   1.407 +	0
   1.408 +	};
   1.409 +
   1.410 +const CPolicyServer::TPolicyElement gServerPolicy9Elements[] =
   1.411 +	{
   1.412 +	{_INIT_SECURITY_POLICY_FAIL,CPolicyServer::EFailClient},
   1.413 +	};
   1.414 +
   1.415 +const CPolicyServer::TPolicy gServerPolicy9 = 
   1.416 +	{
   1.417 +	0,gServerPolicy9RangeCount,
   1.418 +	gServerPolicy9Ranges,
   1.419 +	gServerPolicy9ElementsIndex,
   1.420 +	gServerPolicy9Elements
   1.421 +	};
   1.422 +
   1.423 +//
   1.424 +// EPolicy10
   1.425 +//
   1.426 +
   1.427 +const TUint gServerPolicy10RangeCount = 13;
   1.428 +const TInt gServerPolicy10Ranges[gServerPolicy10RangeCount] = { 
   1.429 +	0, //ECustomCheck
   1.430 +	5, //EAlwaysPass
   1.431 +	6, //ENotSupported
   1.432 +	8, //->0
   1.433 +	9, //->3
   1.434 +	10, //->2
   1.435 +	11, //->1
   1.436 +	12, //ENotSupported
   1.437 +	55, //->3
   1.438 +	58, //ENotSupported
   1.439 +	100, //EAlwaysPass -> Shutdown
   1.440 +	101, //ENotSupported
   1.441 +	KMaxTInt, //EAlways Pass
   1.442 +	};
   1.443 +
   1.444 +const TUint8 gServerPolicy10ElementsIndex[] = { 
   1.445 +	CPolicyServer::ECustomCheck, 
   1.446 +	CPolicyServer::EAlwaysPass, 
   1.447 +	CPolicyServer::ENotSupported,
   1.448 +	0, //RequireNetworkControl or EFail
   1.449 +	3, //RequireDiskAdmin or EQueryUser
   1.450 +	2, //RequireDiskAdmin or EFail
   1.451 +	1, //RequireNetworkControl or EQueryUser
   1.452 +	CPolicyServer::ENotSupported,
   1.453 +	3,
   1.454 +	CPolicyServer::ENotSupported,
   1.455 +	CPolicyServer::EAlwaysPass, 
   1.456 +	CPolicyServer::ENotSupported,
   1.457 +	CPolicyServer::EAlwaysPass, 
   1.458 +	};
   1.459 +
   1.460 +const CPolicyServer::TPolicyElement gServerPolicy10Elements[] =
   1.461 +	{
   1.462 +	{_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl),CPolicyServer::EFailClient},
   1.463 +	{_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl),KQueryUser},
   1.464 +	{_INIT_SECURITY_POLICY_C1(ECapabilityDiskAdmin),CPolicyServer::EFailClient},
   1.465 +	{_INIT_SECURITY_POLICY_C1(ECapabilityDiskAdmin),KQueryUser},
   1.466 +	};
   1.467 +
   1.468 +const CPolicyServer::TPolicy gServerPolicy10 = 
   1.469 +	{
   1.470 +	CPolicyServer::EAlwaysPass,gServerPolicy10RangeCount,
   1.471 +	gServerPolicy10Ranges,
   1.472 +	gServerPolicy10ElementsIndex,
   1.473 +	gServerPolicy10Elements,
   1.474 +	};
   1.475 +
   1.476 +
   1.477 +//A list of all the global policies
   1.478 +const CPolicyServer::TPolicy* gPolicyIndex[] = {
   1.479 +	&gServerPolicy1,
   1.480 +	&gServerPolicy2,
   1.481 +	&gServerPolicy3,
   1.482 +	&gServerPolicy4,
   1.483 +	&gServerPolicy5,
   1.484 +	&gServerPolicy6,
   1.485 +	&gServerPolicy7,
   1.486 +	&gServerPolicy8,
   1.487 +	&gServerPolicy9,
   1.488 +	&gServerPolicy10,
   1.489 +	};
   1.490 +
   1.491 +
   1.492 +#include "testprocess.h"
   1.493 +
   1.494 +TInt StartServer(const CPolicyServer::TPolicy& aPolicy, TUint aServerIndex);
   1.495 +
   1.496 +TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
   1.497 +	{
   1.498 +	switch(aTestNum)
   1.499 +		{
   1.500 +
   1.501 +	case ETestProcessPolicyServer:
   1.502 +		{
   1.503 +		__ASSERT_ALWAYS(aArg1 >= 0 && aArg1 < (TInt)sizeof(gPolicyIndex)>>2, User::Panic(KPolSvr, KErrArgument));
   1.504 +		const CPolicyServer::TPolicy& policy = *(gPolicyIndex[aArg1]);
   1.505 +		__ASSERT_ALWAYS(aArg2 >= 0, User::Panic(KPolSvr, KErrArgument));
   1.506 +		TInt r;
   1.507 +		r=StartServer(policy, TUint(aArg2));
   1.508 +		if(r==KErrAlreadyExists)
   1.509 +			{
   1.510 +			User::After(2*1000*1000);
   1.511 +			r=StartServer(policy, TUint(aArg2));
   1.512 +			}
   1.513 +		return r;
   1.514 +		}
   1.515 +
   1.516 +	default:
   1.517 +		User::Panic(_L("T_POLSVR"),1);
   1.518 +		}
   1.519 +
   1.520 +	return KErrNone;
   1.521 +	}
   1.522 +
   1.523 +
   1.524 +//
   1.525 +// RTestThread
   1.526 +//
   1.527 +
   1.528 +class RTestThread : public RThread
   1.529 +	{
   1.530 +public:
   1.531 +	void Create(TThreadFunction aFunction,TAny* aArg=0);
   1.532 +	};
   1.533 +
   1.534 +void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
   1.535 +	{
   1.536 +	TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
   1.537 +	test(r==KErrNone);
   1.538 +	}
   1.539 +
   1.540 +//
   1.541 +// CTestSession1
   1.542 +//
   1.543 +
   1.544 +class CTestSession1 : public CSession2
   1.545 +	{
   1.546 +public:
   1.547 +	enum {EShutdown=100};
   1.548 +public:
   1.549 +	CTestSession1();
   1.550 +	virtual void ServiceL(const RMessage2& aMsg);
   1.551 +public:
   1.552 +	};
   1.553 +
   1.554 +CTestSession1::CTestSession1()
   1.555 +	: CSession2()
   1.556 +	{}
   1.557 +
   1.558 +
   1.559 +void CTestSession1::ServiceL(const RMessage2& aMsg)
   1.560 +	{
   1.561 +	OrInFlags(aMsg, KServiceLMask);
   1.562 +	TInt fn = aMsg.Function();
   1.563 +	switch(fn)
   1.564 +		{
   1.565 +		case 2:
   1.566 +		case 4:
   1.567 +		case 5:
   1.568 +		case 6:
   1.569 +		case 8:
   1.570 +		case 9:
   1.571 +		case 12:
   1.572 +		case KMaxTInt:
   1.573 +			aMsg.Complete(KErrNone);
   1.574 +			break;
   1.575 +
   1.576 +		case CTestSession1::EShutdown:
   1.577 +			CActiveScheduler::Stop();
   1.578 +			aMsg.Complete(KErrNone);
   1.579 +			break;
   1.580 +		default:
   1.581 +			//If we get here we have an unhandled condition in the test code.
   1.582 +			//The test code is specifically setup to try and catch all branches
   1.583 +			//through the policy server.  If you get, there is some problem in
   1.584 +			//the setup.
   1.585 +			test(0);
   1.586 +			break;
   1.587 +		}
   1.588 +	}
   1.589 +
   1.590 +//
   1.591 +// CTestPolicyServer1
   1.592 +//
   1.593 +
   1.594 +class CTestPolicyServer1 : public CPolicyServer
   1.595 +	{
   1.596 +public:
   1.597 +	CTestPolicyServer1(TInt aPriority, const TPolicy& aPolicy);
   1.598 +	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMsg) const;
   1.599 +	virtual TCustomResult CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction, TSecurityInfo& aMissing);
   1.600 +	virtual TCustomResult CustomFailureActionL(const RMessage2& aMsg, TInt aAction, const TSecurityInfo& aMissing);
   1.601 +	};
   1.602 +
   1.603 +CTestPolicyServer1::CTestPolicyServer1(TInt aPriority, const TPolicy& aPolicy)
   1.604 +	: CPolicyServer(aPriority, aPolicy)
   1.605 +	{
   1.606 +	}
   1.607 +
   1.608 +CSession2* CTestPolicyServer1::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMsg*/) const
   1.609 +	{
   1.610 +	return new (ELeave) CTestSession1();
   1.611 +	}
   1.612 +
   1.613 +CPolicyServer::TCustomResult CTestPolicyServer1::CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction, TSecurityInfo& aMissing)
   1.614 +	{
   1.615 +	TInt fn = aMsg.Function();
   1.616 +	if(fn >= 0)
   1.617 +		{
   1.618 +		//Connect messages don't use this debugging system
   1.619 +		OrInFlags(aMsg, KCustomCheckMask);
   1.620 +		}
   1.621 +	if(fn == -1) //Connect
   1.622 +		{
   1.623 +		return EPass;
   1.624 +		}
   1.625 +	else if(fn == 1)
   1.626 +		{
   1.627 +		aMissing.iCaps.AddCapability(ECapabilityCommDD);
   1.628 +		return EFail;
   1.629 +		}
   1.630 +	else if(fn == 2)
   1.631 +		{
   1.632 +		return EPass;
   1.633 +		}
   1.634 +	else if(fn == 3)
   1.635 +		{
   1.636 +		aMissing.iCaps.AddCapability(ECapabilityCommDD);
   1.637 +		aAction = KQueryUser;
   1.638 +		return EFail;
   1.639 +		}
   1.640 +	else if(fn == 4)
   1.641 +		{
   1.642 +		aMissing.iCaps.AddCapability(ECapabilityCommDD);
   1.643 +		aAction = KQueryUser;
   1.644 +		return EFail;
   1.645 +		}
   1.646 +	else if(fn == 5)
   1.647 +		{
   1.648 +		//Since we are returning ETrue here, setting the action shouldn't affect
   1.649 +		//anything.  This should result in the same as 2.
   1.650 +		aAction = KQueryUser;
   1.651 +		return EPass;
   1.652 +		}
   1.653 +	//If we get here we have an unhandled condition in the test code.  The test
   1.654 +	//code is specifically setup to try and catch all branches through the
   1.655 +	//policy server.  If you get, there is some problem in the setup.
   1.656 +	test(0);
   1.657 +	return EFail;
   1.658 +	}
   1.659 +
   1.660 +CPolicyServer::TCustomResult CTestPolicyServer1::CustomFailureActionL(const RMessage2& aMsg, TInt aAction, const TSecurityInfo& aMissing)
   1.661 +	{
   1.662 +	(void)aMissing;
   1.663 +	(void)aMsg;
   1.664 +	(void)aAction;
   1.665 +	TInt fn = aMsg.Function();
   1.666 +	if(fn >= 0)
   1.667 +		{
   1.668 +		//Connect messages don't use this debugging system
   1.669 +		OrInFlags(aMsg, KCustomActionMask);
   1.670 +		}
   1.671 +	switch(fn)
   1.672 +		{
   1.673 +		case 3:
   1.674 +			return EFail;
   1.675 +		case 4:
   1.676 +			return EPass;
   1.677 +		case 11:
   1.678 +			return EFail;
   1.679 +		case 12:
   1.680 +			return EPass;
   1.681 +		default:
   1.682 +			break;
   1.683 +		}
   1.684 +
   1.685 +	//If we get here we have an unhandled condition in the test code.  The test
   1.686 +	//code is specifically setup to try and catch all branches through the
   1.687 +	//policy server.  If you get, there is some problem in the setup.
   1.688 +	test(0);
   1.689 +	return EFail;
   1.690 +	}
   1.691 +
   1.692 +//
   1.693 +// CTestPolicyServer2
   1.694 +//
   1.695 +
   1.696 +class CTestPolicyServer2 : public CPolicyServer
   1.697 +	{
   1.698 +public:
   1.699 +	CTestPolicyServer2(TInt aPriority, const TPolicy& aPolicy);
   1.700 +	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMsg) const;
   1.701 +	//virtual TBool CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction);
   1.702 +	//virtual TBool CustomFailureActionL(const TSecurityPolicy* aPolicy, const RMessage2& aMsg, TInt aAction);
   1.703 +	};
   1.704 +
   1.705 +//
   1.706 +// CTestSession2
   1.707 +//
   1.708 +
   1.709 +class CTestSession2 : public CSession2
   1.710 +	{
   1.711 +public:
   1.712 +	enum {EShutdown=100};
   1.713 +public:
   1.714 +	CTestSession2();
   1.715 +	virtual void ServiceL(const RMessage2& aMsg);
   1.716 +public:
   1.717 +	};
   1.718 +
   1.719 +CTestSession2::CTestSession2()
   1.720 +	: CSession2()
   1.721 +	{
   1.722 +	}
   1.723 +
   1.724 +void CTestSession2::ServiceL(const RMessage2& aMsg)
   1.725 +	{
   1.726 +	TInt fn = aMsg.Function();
   1.727 +	switch(fn)
   1.728 +		{
   1.729 +		case CTestSession2::EShutdown:
   1.730 +			CActiveScheduler::Stop();
   1.731 +			aMsg.Complete(KErrNone);
   1.732 +			break;
   1.733 +		default:
   1.734 +			//If we get here we have an unhandled condition in the test code.
   1.735 +			//The test code is specifically setup to try and catch all branches
   1.736 +			//through the policy server.  If you get, there is some problem in
   1.737 +			//the setup.
   1.738 +			test(0);
   1.739 +			break;
   1.740 +		}
   1.741 +	}
   1.742 +
   1.743 +CTestPolicyServer2::CTestPolicyServer2(TInt aPriority, const TPolicy& aPolicy)
   1.744 +	: CPolicyServer(aPriority, aPolicy)
   1.745 +	{
   1.746 +	}
   1.747 +
   1.748 +CSession2* CTestPolicyServer2::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMsg*/) const
   1.749 +	{
   1.750 +	return new (ELeave) CTestSession2();
   1.751 +	}
   1.752 +
   1.753 +//
   1.754 +// CTestPolicyServer3
   1.755 +//
   1.756 +
   1.757 +class CTestPolicyServer3 : public CPolicyServer
   1.758 +	{
   1.759 +public:
   1.760 +	CTestPolicyServer3(TInt aPriority, const TPolicy& aPolicy);
   1.761 +	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMsg) const;
   1.762 +	virtual TCustomResult CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction, TSecurityInfo&);
   1.763 +	virtual TCustomResult CustomFailureActionL(const RMessage2& aMsg, TInt aAction, const TSecurityInfo& aMissing);
   1.764 +	};
   1.765 +
   1.766 +CPolicyServer::TCustomResult CTestPolicyServer3::CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction, TSecurityInfo& aMissing)
   1.767 +	{
   1.768 +	(void)aAction;
   1.769 +	(void)aMsg;
   1.770 +	(void)aMissing;
   1.771 +	//If we get here we have an unhandled condition in the test code.  The test
   1.772 +	//code is specifically setup to try and catch all branches through the
   1.773 +	//policy server.  If you get, there is some problem in the setup.
   1.774 +	test(0);
   1.775 +	return EFail;
   1.776 +	}
   1.777 +
   1.778 +CPolicyServer::TCustomResult CTestPolicyServer3::CustomFailureActionL(const RMessage2& aMsg, TInt aAction, const TSecurityInfo& aMissing)
   1.779 +	{
   1.780 +	(void)aMissing;
   1.781 +	(void)aMsg;
   1.782 +	(void)aAction;
   1.783 +	TInt fn = aMsg.Function();
   1.784 +	switch(fn)
   1.785 +		{
   1.786 +		case -1:
   1.787 +			return EPass;
   1.788 +		default:
   1.789 +			break;
   1.790 +		}
   1.791 +
   1.792 +	//If we get here we have an unhandled condition in the test code.  The test
   1.793 +	//code is specifically setup to try and catch all branches through the
   1.794 +	//policy server.  If you get, there is some problem in the setup.
   1.795 +	test(0);
   1.796 +	return EFail;
   1.797 +	}
   1.798 +
   1.799 +//
   1.800 +// CTestSession3
   1.801 +//
   1.802 +
   1.803 +class CTestSession3 : public CSession2
   1.804 +	{
   1.805 +public:
   1.806 +	enum {EShutdown=100};
   1.807 +public:
   1.808 +	CTestSession3();
   1.809 +	virtual void ServiceL(const RMessage2& aMsg);
   1.810 +public:
   1.811 +	};
   1.812 +
   1.813 +CTestSession3::CTestSession3()
   1.814 +	: CSession2()
   1.815 +	{
   1.816 +	}
   1.817 +
   1.818 +void CTestSession3::ServiceL(const RMessage2& aMsg)
   1.819 +	{
   1.820 +	TInt fn = aMsg.Function();
   1.821 +	switch(fn)
   1.822 +		{
   1.823 +		case CTestSession3::EShutdown:
   1.824 +			CActiveScheduler::Stop();
   1.825 +			aMsg.Complete(KErrNone);
   1.826 +			break;
   1.827 +		default:
   1.828 +			//If we get here we have an unhandled condition in the test code.
   1.829 +			//The test code is specifically setup to try and catch all branches
   1.830 +			//through the policy server.  If you get, there is some problem in
   1.831 +			//the setup.
   1.832 +			test(0);
   1.833 +			break;
   1.834 +		}
   1.835 +	}
   1.836 +
   1.837 +CTestPolicyServer3::CTestPolicyServer3(TInt aPriority, const TPolicy& aPolicy)
   1.838 +	: CPolicyServer(aPriority, aPolicy)
   1.839 +	{
   1.840 +	}
   1.841 +
   1.842 +CSession2* CTestPolicyServer3::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMsg*/) const
   1.843 +	{
   1.844 +	return new (ELeave) CTestSession3();
   1.845 +	}
   1.846 +
   1.847 +_LIT(KCustomActive, "CCustomActive");
   1.848 +//
   1.849 +// CCustomActive
   1.850 +//
   1.851 +
   1.852 +class CCustomActive : public CActive
   1.853 +	{
   1.854 +public:
   1.855 +	static CCustomActive* NewL(CPolicyServer& aServer);
   1.856 +	void RunL();
   1.857 +	TInt RunError(TInt aError);
   1.858 +	void DoCancel();
   1.859 +	void CustomSecurityCheckL(const RMessage2& aMsg, TInt aAction);
   1.860 +	void CustomFailureActionL(const RMessage2& aMsg, TInt aAction);
   1.861 +protected:
   1.862 +	void HandleSecurityCheckResultL();
   1.863 +	void HandleFailureActionResultL();
   1.864 +	void ConstructL();	
   1.865 +private:
   1.866 +	CCustomActive(CPolicyServer& aServer);
   1.867 +	RTimer iTimer;
   1.868 +	CPolicyServer& iServer;
   1.869 +	const RMessage2* iMsg;
   1.870 +	enum TState {
   1.871 +		ECustomSecurityCheck,
   1.872 +		EFailureAction,
   1.873 +		};
   1.874 +	TState iState;
   1.875 +	TInt iAction;
   1.876 +	TSecurityInfo iMissing;
   1.877 +	};
   1.878 +
   1.879 +CCustomActive::CCustomActive(CPolicyServer& aServer)
   1.880 +	: CActive(0), iServer(aServer), iMsg(0), iState(ECustomSecurityCheck)
   1.881 +	{
   1.882 +	CActiveScheduler::Add(this);
   1.883 +	}
   1.884 +
   1.885 +CCustomActive* CCustomActive::NewL(CPolicyServer& aServer)
   1.886 +	{
   1.887 +	CCustomActive* self = new(ELeave)CCustomActive(aServer);
   1.888 +	CleanupStack::PushL(self);
   1.889 +	self->ConstructL();
   1.890 +	CleanupStack::Pop(self);
   1.891 +	return self;
   1.892 +	}
   1.893 +
   1.894 +void CCustomActive::ConstructL()
   1.895 +	{
   1.896 +	User::LeaveIfError(iTimer.CreateLocal());
   1.897 +	memset(&iMissing, 0, sizeof(TSecurityInfo));
   1.898 +	}
   1.899 +
   1.900 +void CCustomActive::HandleSecurityCheckResultL()
   1.901 +	{
   1.902 +	TInt fn = iMsg->Function();
   1.903 +	switch(fn)
   1.904 +		{
   1.905 +		case 0:
   1.906 +			iStatus = KErrGeneral;
   1.907 +			break;
   1.908 +		case 1: //pass
   1.909 +		case 4:
   1.910 +			break;
   1.911 +		case 2:
   1.912 +		case 3:
   1.913 +			iStatus = KErrGeneral;
   1.914 +			iAction = KQueryUser;
   1.915 +			break;
   1.916 +		default:
   1.917 +			test(0);
   1.918 +		}
   1.919 +	if(iStatus == KErrNone)
   1.920 +		{
   1.921 +		iServer.ProcessL(*iMsg);
   1.922 +		iMsg=0;
   1.923 +		}
   1.924 +	else
   1.925 +		{
   1.926 +		const RMessage2* ptr = iMsg;
   1.927 +		iMsg=0;
   1.928 +		iServer.CheckFailedL(*ptr, iAction, iMissing);
   1.929 +		}
   1.930 +	}
   1.931 +
   1.932 +void CCustomActive::HandleFailureActionResultL()
   1.933 +	{
   1.934 +	TInt fn = iMsg->Function();
   1.935 +	switch(fn)
   1.936 +		{
   1.937 +		case 2:
   1.938 +			iStatus = KErrGeneral;
   1.939 +			break;
   1.940 +		case 3: //pass
   1.941 +		case 9:
   1.942 +		case 57: //passes through so ServiceL can leave with NoMem
   1.943 +			break;
   1.944 +		case 56:
   1.945 +			User::Leave(KErrNoMemory);
   1.946 +			break;
   1.947 +			
   1.948 +		default:
   1.949 +			test(0);
   1.950 +		}
   1.951 +	if(iStatus == KErrNone)
   1.952 +		iServer.ProcessL(*iMsg);
   1.953 +	else
   1.954 +		iMsg->Complete(KErrPermissionDenied);
   1.955 +	iMsg=0;
   1.956 +	}
   1.957 +
   1.958 +void CCustomActive::RunL()
   1.959 +	{
   1.960 +	OrInFlags(*iMsg, KActiveRunLMask);
   1.961 +
   1.962 +	switch(iState)
   1.963 +		{
   1.964 +		case ECustomSecurityCheck:
   1.965 +			HandleSecurityCheckResultL();
   1.966 +			break;
   1.967 +		case EFailureAction:
   1.968 +			HandleFailureActionResultL();
   1.969 +			break;
   1.970 +		default:
   1.971 +			//Invalid state
   1.972 +			User::Panic(KCustomActive, 11);
   1.973 +			break;
   1.974 +		}
   1.975 +	}
   1.976 +
   1.977 +TInt CCustomActive::RunError(TInt aError)
   1.978 +	{
   1.979 +	OrInFlags(*iMsg, KActiveRunErrorMask);
   1.980 +	if(iMsg)
   1.981 +		{
   1.982 +		iServer.ProcessError(*iMsg, aError);
   1.983 +		iMsg = 0;
   1.984 +		iAction = CPolicyServer::EFailClient;
   1.985 +		return KErrNone;
   1.986 +		}
   1.987 +	else
   1.988 +		return aError;
   1.989 +	}
   1.990 +
   1.991 +void CCustomActive::DoCancel()
   1.992 +	{
   1.993 +	OrInFlags(*iMsg, KActiveDoCancelMask);
   1.994 +	iTimer.Cancel();
   1.995 +	if(iMsg)
   1.996 +		{
   1.997 +		iMsg->Complete(KErrCancel);
   1.998 +		}
   1.999 +	iMsg = 0;
  1.1000 +	iAction = CPolicyServer::EFailClient;
  1.1001 +	}
  1.1002 +
  1.1003 +void CCustomActive::CustomSecurityCheckL(const RMessage2& aMsg, TInt aAction)
  1.1004 +	{
  1.1005 +	__ASSERT_ALWAYS(!IsActive(), User::Panic(KCustomActive, 1));
  1.1006 +	__ASSERT_ALWAYS(iMsg == 0, User::Panic(KCustomActive, 2));
  1.1007 +
  1.1008 +	OrInFlags(aMsg, KActiveCheckMask);
  1.1009 +
  1.1010 +	iTimer.After(iStatus, 100000);
  1.1011 +	SetActive();
  1.1012 +	iMsg = &aMsg;
  1.1013 +	iState = ECustomSecurityCheck;
  1.1014 +	iAction = aAction;
  1.1015 +	}
  1.1016 +
  1.1017 +void CCustomActive::CustomFailureActionL(const RMessage2& aMsg, TInt aAction)
  1.1018 +	{
  1.1019 +	__ASSERT_ALWAYS(!IsActive(), User::Panic(KCustomActive, 3));
  1.1020 +	__ASSERT_ALWAYS(iMsg == 0, User::Panic(KCustomActive, 4));
  1.1021 +
  1.1022 +	OrInFlags(aMsg, KActiveActionMask);
  1.1023 +
  1.1024 +	iTimer.After(iStatus, 50000);
  1.1025 +	SetActive();
  1.1026 +	iMsg = &aMsg;
  1.1027 +	iState = EFailureAction;
  1.1028 +	iAction = aAction;
  1.1029 +
  1.1030 +	if(aMsg.Function() == 55)
  1.1031 +		{
  1.1032 +		Cancel();
  1.1033 +		}
  1.1034 +	}
  1.1035 +
  1.1036 +//
  1.1037 +// CTestPolicyServer4
  1.1038 +//
  1.1039 +
  1.1040 +class CTestPolicyServer4 : public CPolicyServer
  1.1041 +	{
  1.1042 +public:
  1.1043 +	static CTestPolicyServer4* NewL(TInt aPriority, const TPolicy& aPolicy);
  1.1044 +	CTestPolicyServer4(TInt aPriority, const TPolicy& aPolicy);
  1.1045 +	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMsg) const;
  1.1046 +	virtual TCustomResult CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction, TSecurityInfo& aMissing);
  1.1047 +	virtual TCustomResult CustomFailureActionL(const RMessage2& aMsg, TInt aAction, const TSecurityInfo& aMissing);
  1.1048 +	CCustomActive* iActiveCheck;
  1.1049 +protected:
  1.1050 +	void ConstructL();
  1.1051 +	};
  1.1052 +
  1.1053 +CTestPolicyServer4* CTestPolicyServer4::NewL(TInt aPriority, const TPolicy& aPolicy)
  1.1054 +	{
  1.1055 +	CTestPolicyServer4* self = new(ELeave)CTestPolicyServer4(aPriority, aPolicy);
  1.1056 +	CleanupStack::PushL(self);
  1.1057 +	self->ConstructL();
  1.1058 +	CleanupStack::Pop(self);
  1.1059 +	return self;
  1.1060 +	}
  1.1061 +
  1.1062 +void CTestPolicyServer4::ConstructL()
  1.1063 +	{
  1.1064 +	iActiveCheck = CCustomActive::NewL(*this);
  1.1065 +	}
  1.1066 +
  1.1067 +CPolicyServer::TCustomResult CTestPolicyServer4::CustomSecurityCheckL(const RMessage2& aMsg, TInt& aAction, TSecurityInfo& aMissing)
  1.1068 +	{
  1.1069 +	(void)aMissing;
  1.1070 +	OrInFlags(aMsg, KCustomCheckMask);
  1.1071 +	iActiveCheck->CustomSecurityCheckL(aMsg, aAction);
  1.1072 +	return EAsync;
  1.1073 +	}
  1.1074 +
  1.1075 +CPolicyServer::TCustomResult CTestPolicyServer4::CustomFailureActionL(const RMessage2& aMsg, TInt aAction, const TSecurityInfo& aMissing)
  1.1076 +	{
  1.1077 +	(void)aMissing;
  1.1078 +	OrInFlags(aMsg, KCustomActionMask);
  1.1079 +	iActiveCheck->CustomFailureActionL(aMsg, aAction);
  1.1080 +	return EAsync;
  1.1081 +	}
  1.1082 +
  1.1083 +//
  1.1084 +// CTestSession4
  1.1085 +//
  1.1086 +
  1.1087 +class CTestSession4 : public CSession2
  1.1088 +	{
  1.1089 +public:
  1.1090 +	enum {EShutdown=100};
  1.1091 +public:
  1.1092 +	CTestSession4();
  1.1093 +	virtual void ServiceL(const RMessage2& aMsg);
  1.1094 +	virtual void ServiceError(const RMessage2& aMsg, TInt aError);
  1.1095 +public:
  1.1096 +	};
  1.1097 +
  1.1098 +CTestSession4::CTestSession4()
  1.1099 +	: CSession2()
  1.1100 +	{
  1.1101 +	}
  1.1102 +
  1.1103 +void CTestSession4::ServiceL(const RMessage2& aMsg)
  1.1104 +	{
  1.1105 +	TInt fn = aMsg.Function();
  1.1106 +	OrInFlags(aMsg, KServiceLMask);
  1.1107 +	switch(fn)
  1.1108 +		{
  1.1109 +		case 1:
  1.1110 +		case 3:
  1.1111 +		case 4:
  1.1112 +		case 5:
  1.1113 +		case 8:
  1.1114 +		case 9:
  1.1115 +		case 11:
  1.1116 +			aMsg.Complete(KErrNone);
  1.1117 +			break;
  1.1118 +		case 57:
  1.1119 +			User::Leave(KErrNoMemory);
  1.1120 +			break;
  1.1121 +		case CTestSession4::EShutdown:
  1.1122 +			CActiveScheduler::Stop();
  1.1123 +			aMsg.Complete(KErrNone);
  1.1124 +			break;
  1.1125 +		case KMaxTInt:
  1.1126 +			//KMaxTInt would otherwise interfere with the OrInFlags value, so
  1.1127 +			//we return our location this way instead.
  1.1128 +			aMsg.Complete(KMaxTInt);
  1.1129 +			break;
  1.1130 +		default:
  1.1131 +			//If we get here we have an unhandled condition in the test code.
  1.1132 +			//The test code is specifically setup to try and catch all branches
  1.1133 +			//through the policy server.  If you get, there is some problem in
  1.1134 +			//the setup.
  1.1135 +			test(0);
  1.1136 +			break;
  1.1137 +		}
  1.1138 +	}
  1.1139 +
  1.1140 +void CTestSession4::ServiceError(const RMessage2& aMsg, TInt aError)
  1.1141 +	{
  1.1142 +	OrInFlags(aMsg, KServiceErrorMask);
  1.1143 +	if(!aMsg.IsNull())
  1.1144 +		aMsg.Complete(aError);
  1.1145 +	}
  1.1146 +
  1.1147 +CTestPolicyServer4::CTestPolicyServer4(TInt aPriority, const TPolicy& aPolicy)
  1.1148 +	: CPolicyServer(aPriority, aPolicy)
  1.1149 +	{
  1.1150 +	}
  1.1151 +
  1.1152 +CSession2* CTestPolicyServer4::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMsg*/) const
  1.1153 +	{
  1.1154 +	return new (ELeave) CTestSession4();
  1.1155 +	}
  1.1156 +
  1.1157 +
  1.1158 +//
  1.1159 +// CTestActiveScheduler
  1.1160 +//
  1.1161 +
  1.1162 +class CTestActiveScheduler : public CActiveScheduler
  1.1163 +	{
  1.1164 +public:
  1.1165 +	virtual void Error(TInt anError) const;
  1.1166 +	};
  1.1167 +
  1.1168 +void CTestActiveScheduler::Error(TInt anError) const
  1.1169 +	{
  1.1170 +	User::Panic(_L("TestServer Error"),anError);
  1.1171 +	}
  1.1172 +
  1.1173 +
  1.1174 +
  1.1175 +//
  1.1176 +// Server thread
  1.1177 +//
  1.1178 +
  1.1179 +_LIT(KServerName,"T_POLSVR-server");
  1.1180 +const TInt KServerRendezvous = KRequestPending+1;
  1.1181 +
  1.1182 +void DoStartServer(const CPolicyServer::TPolicy& aPolicy, TUint aServerIndex)
  1.1183 +	{
  1.1184 +	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
  1.1185 +	CActiveScheduler::Install(activeScheduler);
  1.1186 +	CleanupStack::PushL(activeScheduler);
  1.1187 +
  1.1188 +	CPolicyServer* server = 0;
  1.1189 +	switch(aServerIndex)
  1.1190 +		{
  1.1191 +		case 0:
  1.1192 +			server = new (ELeave) CTestPolicyServer1(0,aPolicy);
  1.1193 +			break;
  1.1194 +		case 1:
  1.1195 +			server = new (ELeave) CTestPolicyServer2(0,aPolicy);
  1.1196 +			break;
  1.1197 +		case 2:
  1.1198 +			server = new (ELeave) CTestPolicyServer3(0,aPolicy);
  1.1199 +			break;
  1.1200 +		case 3:
  1.1201 +			server = CTestPolicyServer4::NewL(0,aPolicy);
  1.1202 +			break;
  1.1203 +		default:
  1.1204 +			User::Panic(KPolSvr, KErrArgument);
  1.1205 +		}
  1.1206 +	CleanupStack::PushL(server);
  1.1207 +
  1.1208 +	User::LeaveIfError(server->Start(KServerName));
  1.1209 +
  1.1210 +	RProcess::Rendezvous(KServerRendezvous);
  1.1211 +
  1.1212 +	CActiveScheduler::Start();
  1.1213 +
  1.1214 +	CleanupStack::PopAndDestroy(2);
  1.1215 +	}
  1.1216 +
  1.1217 +TInt StartServer(const CPolicyServer::TPolicy& aPolicy, TUint aServerIndex)
  1.1218 +	{
  1.1219 +	CTrapCleanup* cleanupStack = CTrapCleanup::New();
  1.1220 +	if(!cleanupStack)
  1.1221 +		return KErrNoMemory;
  1.1222 +	TRAPD(leaveError,DoStartServer(aPolicy,aServerIndex))
  1.1223 +	delete cleanupStack;
  1.1224 +	return leaveError;
  1.1225 +	}
  1.1226 +
  1.1227 +
  1.1228 +
  1.1229 +//
  1.1230 +// RTestSession
  1.1231 +//
  1.1232 +
  1.1233 +class RTestSession : public RSessionBase
  1.1234 +	{
  1.1235 +public:
  1.1236 +	inline TInt Connect()
  1.1237 +		{ return CreateSession(KServerName,TVersion());}
  1.1238 +//	inline TInt Send(TInt aFunction)
  1.1239 +//		{ return RSessionBase::SendReceive(aFunction); }
  1.1240 +	inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
  1.1241 +		{ return RSessionBase::SendReceive(aFunction,aArgs); }
  1.1242 +	inline void Send(TInt aFunction,TRequestStatus& aStatus)
  1.1243 +		{ RSessionBase::SendReceive(aFunction,aStatus); }
  1.1244 +	inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
  1.1245 +		{ RSessionBase::SendReceive(aFunction,aArgs,aStatus); }
  1.1246 +	};
  1.1247 +
  1.1248 +
  1.1249 +
  1.1250 +RTestSession Session;
  1.1251 +
  1.1252 +#include <e32svr.h>
  1.1253 +
  1.1254 +void TestServer1WithPolicy1()
  1.1255 +	{
  1.1256 +	RTestProcess server;
  1.1257 +	TRequestStatus rendezvous;
  1.1258 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy1,ETestServer1);
  1.1259 +	server.Rendezvous(rendezvous);
  1.1260 +	server.Resume();
  1.1261 +	User::WaitForRequest(rendezvous);
  1.1262 +	test(rendezvous==KServerRendezvous);
  1.1263 +
  1.1264 +	test.Next(_L("Server 1, Policy 1"));
  1.1265 +	TInt r = Session.Connect();
  1.1266 +	test(r==KErrNone);
  1.1267 +
  1.1268 +	TBuf8<4> flags(4);
  1.1269 +
  1.1270 +	//case 0: Not Supported, returned from policy server level.
  1.1271 +	SetFlags(flags, 0);
  1.1272 +	r = Session.Send(0, TIpcArgs(&flags));
  1.1273 +	test(r==KErrNotSupported);
  1.1274 +	test(FlagsValue(flags) == (0));
  1.1275 +
  1.1276 +	//case 1: Custom Check, fails with KErrPermissionDenied
  1.1277 +	SetFlags(flags, 1);
  1.1278 +	r = Session.Send(1, TIpcArgs(&flags));
  1.1279 +	test(r==KErrPermissionDenied);
  1.1280 +	test(FlagsValue(flags) == (1 | KCustomCheckMask) ); 
  1.1281 +
  1.1282 +	//case 2: Custom Check passes.
  1.1283 +	SetFlags(flags, 2);
  1.1284 +	r = Session.Send(2, TIpcArgs(&flags));
  1.1285 +	test(r==KErrNone);
  1.1286 +	test(FlagsValue(flags) == (2 | KCustomCheckMask | KServiceLMask) ); 
  1.1287 +
  1.1288 +	//case 3: Custom Check fails but action set to EQueryUser, query
  1.1289 +	//subsequently fails
  1.1290 +	SetFlags(flags, 3);
  1.1291 +	r = Session.Send(3, TIpcArgs(&flags));
  1.1292 +	test(r==KErrPermissionDenied);
  1.1293 +	test(FlagsValue(flags) == (3 | KCustomCheckMask | KCustomActionMask ));
  1.1294 +
  1.1295 +	//case 4: Custom Check fails but action set to EQueryUser, query
  1.1296 +	//subsequently passes
  1.1297 +	SetFlags(flags, 4);
  1.1298 +	r = Session.Send(4, TIpcArgs(&flags));
  1.1299 +	test(r==KErrNone);
  1.1300 +	test(FlagsValue(flags) == (4 | KCustomCheckMask | KCustomActionMask | KServiceLMask ));
  1.1301 +	
  1.1302 +	//case 5: Custom Check passes and action is set.  Action set shouldn't make
  1.1303 +	//a difference.  Should be same result as case 2.
  1.1304 +	SetFlags(flags, 5);
  1.1305 +	r = Session.Send(5, TIpcArgs(&flags));
  1.1306 +	test(r==KErrNone);
  1.1307 +	test(FlagsValue(flags) == (5 | KCustomCheckMask | KServiceLMask ));
  1.1308 +
  1.1309 +	//case 6: Always passes at the policy server level.
  1.1310 +	SetFlags(flags, 6);
  1.1311 +	r = Session.Send(6, TIpcArgs(&flags));
  1.1312 +	test(r==KErrNone);
  1.1313 +	test(FlagsValue(flags) == (6 | KServiceLMask) );
  1.1314 +
  1.1315 +	//case 7: Not Supported, returned from policy server level.
  1.1316 +	SetFlags(flags, 7);
  1.1317 +	r = Session.Send(7, TIpcArgs(&flags));
  1.1318 +	test(r==KErrNotSupported);
  1.1319 +	test(FlagsValue(flags) == (7));
  1.1320 +
  1.1321 +	//case 8: Requires NetworkControl, which we have, so it passes.
  1.1322 +	SetFlags(flags, 8);
  1.1323 +	r = Session.Send(8, TIpcArgs(&flags));
  1.1324 +	test(r==KErrNone);
  1.1325 +	test(FlagsValue(flags) == (8 | KServiceLMask));
  1.1326 +
  1.1327 +	//case 9: Requires NetworkControl -> pass.  Thrown in a EQueryUser to see
  1.1328 +	//if it causes any problems -> it shouldn't.  Should be same as case 8.
  1.1329 +	SetFlags(flags, 9);
  1.1330 +	r = Session.Send(9, TIpcArgs(&flags));
  1.1331 +	test(r==KErrNone);
  1.1332 +	test(FlagsValue(flags) == (9 | KServiceLMask));
  1.1333 +
  1.1334 +	//case 10: Requires DiskAdmin which we don't have.  
  1.1335 +	SetFlags(flags, 10);
  1.1336 +	r = Session.Send(10, TIpcArgs(&flags));
  1.1337 +	test(r==KErrPermissionDenied);
  1.1338 +	test(FlagsValue(flags) == (10));
  1.1339 +
  1.1340 +	//case 11: Requires DiskAdmin, which we don't have. EQueryUser is set, and
  1.1341 +	//it fails.
  1.1342 +	SetFlags(flags, 11);
  1.1343 +	r = Session.Send(11, TIpcArgs(&flags));
  1.1344 +	test(r==KErrPermissionDenied);
  1.1345 +	test(FlagsValue(flags) == (11 | KCustomActionMask));
  1.1346 +
  1.1347 +	//case 12: Requires DiskAdmin, which we don't have. EQueryUser is set, and
  1.1348 +	//it passes.
  1.1349 +	SetFlags(flags, 12);
  1.1350 +	r = Session.Send(12, TIpcArgs(&flags));
  1.1351 +	test(r==KErrNone);
  1.1352 +	test(FlagsValue(flags) == (12 | KCustomActionMask | KServiceLMask));
  1.1353 +
  1.1354 +	//case 13: Not Supported, returned from policy server level.
  1.1355 +	SetFlags(flags, 13);
  1.1356 +	r = Session.Send(13, TIpcArgs(&flags));
  1.1357 +	test(r==KErrNotSupported);
  1.1358 +	test(FlagsValue(flags) == (13));
  1.1359 +
  1.1360 +	//case 14: Not Supported, returned from policy server level.
  1.1361 +	SetFlags(flags, 14);
  1.1362 +	r = Session.Send(14, TIpcArgs(&flags));
  1.1363 +	test(r==KErrNotSupported);
  1.1364 +	test(FlagsValue(flags) == (14));
  1.1365 +
  1.1366 +	//case 55: Not Supported, returned from policy server level.
  1.1367 +	SetFlags(flags, 55);
  1.1368 +	r = Session.Send(55, TIpcArgs(&flags));
  1.1369 +	test(r==KErrNotSupported);
  1.1370 +	test(FlagsValue(flags) == (55));
  1.1371 +
  1.1372 +	//case 86: Not Supported, returned from policy server level.
  1.1373 +	SetFlags(flags, 86);
  1.1374 +	r = Session.Send(86, TIpcArgs(&flags));
  1.1375 +	test(r==KErrNotSupported);
  1.1376 +	test(FlagsValue(flags) == (86));
  1.1377 +
  1.1378 +	//case 99: Not Supported, returned from policy server level.
  1.1379 +	SetFlags(flags, 99);
  1.1380 +	r = Session.Send(99, TIpcArgs(&flags));
  1.1381 +	test(r==KErrNotSupported);
  1.1382 +	test(FlagsValue(flags) == (99));
  1.1383 +
  1.1384 +	//case 101: Not Supported, returned from policy server level.
  1.1385 +	SetFlags(flags, 101);
  1.1386 +	r = Session.Send(101, TIpcArgs(&flags));
  1.1387 +	test(r==KErrNotSupported);
  1.1388 +	test(FlagsValue(flags) == (101));
  1.1389 +
  1.1390 +	//case 1000191: Not Supported, returned from policy server level.
  1.1391 +	SetFlags(flags, 1000191);
  1.1392 +	r = Session.Send(1000191, TIpcArgs(&flags));
  1.1393 +	test(r==KErrNotSupported);
  1.1394 +	test(FlagsValue(flags) == (1000191));
  1.1395 +
  1.1396 +	//case 1000848: Not Supported, returned from policy server level.
  1.1397 +	SetFlags(flags, 1000848);
  1.1398 +	r = Session.Send(1000848, TIpcArgs(&flags));
  1.1399 +	test(r==KErrNotSupported);
  1.1400 +	test(FlagsValue(flags) == (1000848));
  1.1401 +
  1.1402 +	//case KMaxTInt-1: Not Supported, returned from policy server level.
  1.1403 +	SetFlags(flags, KMaxTInt-1);
  1.1404 +	r = Session.Send(KMaxTInt-1, TIpcArgs(&flags));
  1.1405 +	test(r==KErrNotSupported);
  1.1406 +	test(FlagsValue(flags) == (KMaxTInt-1));
  1.1407 +
  1.1408 +	//case KMaxTInt: Not Supported, returned from policy server level.
  1.1409 +	SetFlags(flags, KMaxTInt);
  1.1410 +	r = Session.Send(KMaxTInt, TIpcArgs(&flags));
  1.1411 +	test(r==KErrNotSupported);
  1.1412 +	test(FlagsValue(flags) == (TUint)(KMaxTInt));
  1.1413 +
  1.1414 +	r = Session.Send(CTestSession1::EShutdown, TIpcArgs(&flags));
  1.1415 +	test.Printf(_L("r = %d\n"),r);
  1.1416 +	test(r==KErrNone);
  1.1417 +
  1.1418 +	Session.Close();
  1.1419 +	CLOSE_AND_WAIT(server);
  1.1420 +	}
  1.1421 +
  1.1422 +#include <e32panic.h>
  1.1423 +
  1.1424 +void TestServer1WithPolicy2()
  1.1425 +	{
  1.1426 +	test.Next(_L("Server 1, Policy 2"));
  1.1427 +	RTestProcess server;
  1.1428 +	TRequestStatus rendezvous;
  1.1429 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy2,ETestServer1);
  1.1430 +	server.Rendezvous(rendezvous);
  1.1431 +	server.Resume();
  1.1432 +	User::WaitForRequest(rendezvous);
  1.1433 +#ifdef _DEBUG
  1.1434 +	//Debug mode does a policy integrity check
  1.1435 +	test(rendezvous==EPolSvr1stRangeNotZero);
  1.1436 +#else
  1.1437 +	test(rendezvous==KServerRendezvous);
  1.1438 +	server.Terminate(0);
  1.1439 +#endif
  1.1440 +	CLOSE_AND_WAIT(server);
  1.1441 +	}
  1.1442 +
  1.1443 +void TestServer1WithPolicy3()
  1.1444 +	{
  1.1445 +	test.Next(_L("Server 1, Policy 3"));
  1.1446 +	RTestProcess server;
  1.1447 +	TRequestStatus rendezvous;
  1.1448 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy3,ETestServer1);
  1.1449 +	server.Rendezvous(rendezvous);
  1.1450 +	server.Resume();
  1.1451 +	User::WaitForRequest(rendezvous);
  1.1452 +#ifdef _DEBUG
  1.1453 +	//Debug mode does a policy integrity check
  1.1454 +	test(rendezvous==EPolSvrRangesNotIncreasing);
  1.1455 +#else
  1.1456 +	test(rendezvous==KServerRendezvous);
  1.1457 +	server.Terminate(0);
  1.1458 +#endif
  1.1459 +	CLOSE_AND_WAIT(server);
  1.1460 +	}
  1.1461 +
  1.1462 +void TestServer1WithPolicy4()
  1.1463 +	{
  1.1464 +	test.Next(_L("Server 1, Policy 4"));
  1.1465 +	RTestProcess server;
  1.1466 +	TRequestStatus rendezvous;
  1.1467 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy4,ETestServer1);
  1.1468 +	server.Rendezvous(rendezvous);
  1.1469 +	server.Resume();
  1.1470 +	User::WaitForRequest(rendezvous);
  1.1471 +#ifdef _DEBUG
  1.1472 +	//Debug mode does a policy integrity check
  1.1473 +	test(rendezvous==EPolSvrElementsIndexValueInvalid);
  1.1474 +#else
  1.1475 +	test(rendezvous==KServerRendezvous);
  1.1476 +	server.Terminate(0);
  1.1477 +#endif
  1.1478 +	CLOSE_AND_WAIT(server);
  1.1479 +	}
  1.1480 +
  1.1481 +void TestServer1WithPolicy5()
  1.1482 +	{
  1.1483 +	test.Next(_L("Server 1, Policy 5"));
  1.1484 +	RTestProcess server;
  1.1485 +	TRequestStatus rendezvous;
  1.1486 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy5,ETestServer1);
  1.1487 +	server.Rendezvous(rendezvous);
  1.1488 +	server.Resume();
  1.1489 +	User::WaitForRequest(rendezvous);
  1.1490 +#ifdef _DEBUG
  1.1491 +	//Debug mode does a policy integrity check
  1.1492 +	test(rendezvous==EPolSvrElementsIndexValueInvalid);
  1.1493 +#else
  1.1494 +	test(rendezvous==KServerRendezvous);
  1.1495 +	server.Terminate(0);
  1.1496 +#endif
  1.1497 +	CLOSE_AND_WAIT(server);
  1.1498 +	}
  1.1499 +
  1.1500 +void TestServer1WithPolicy6()
  1.1501 +	{
  1.1502 +	test.Next(_L("Server 1, Policy 6"));
  1.1503 +	RTestProcess server;
  1.1504 +	TRequestStatus rendezvous;
  1.1505 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy6,ETestServer1);
  1.1506 +	server.Rendezvous(rendezvous);
  1.1507 +	server.Resume();
  1.1508 +	User::WaitForRequest(rendezvous);
  1.1509 +#ifdef _DEBUG
  1.1510 +	//Debug mode does a policy integrity check
  1.1511 +	test(rendezvous==EPolSvrIOnConnectValueInvalid);
  1.1512 +#else
  1.1513 +	test(rendezvous==KServerRendezvous);
  1.1514 +	server.Terminate(0);
  1.1515 +#endif
  1.1516 +	CLOSE_AND_WAIT(server);
  1.1517 +	}
  1.1518 +
  1.1519 +void TestServer1WithPolicy7()
  1.1520 +	{
  1.1521 +	test.Next(_L("Server 1, Policy 7"));
  1.1522 +	RTestProcess server;
  1.1523 +	TRequestStatus rendezvous;
  1.1524 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy7,ETestServer1);
  1.1525 +	server.Rendezvous(rendezvous);
  1.1526 +	server.Resume();
  1.1527 +	User::WaitForRequest(rendezvous);
  1.1528 +	test(rendezvous==KServerRendezvous);
  1.1529 +
  1.1530 +	TInt r = Session.Connect();
  1.1531 +	test(r==KErrNotSupported);
  1.1532 +
  1.1533 +	//We can do this because we have power management
  1.1534 +	server.Terminate(KErrGeneral);
  1.1535 +	CLOSE_AND_WAIT(server);
  1.1536 +	}
  1.1537 +
  1.1538 +void TestServer1WithPolicy8()
  1.1539 +	{
  1.1540 +	test.Next(_L("Server 1, Policy 8"));
  1.1541 +	RTestProcess server;
  1.1542 +	TRequestStatus rendezvous;
  1.1543 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy8,ETestServer1);
  1.1544 +	server.Rendezvous(rendezvous);
  1.1545 +	server.Resume();
  1.1546 +	User::WaitForRequest(rendezvous);
  1.1547 +	test(rendezvous==KServerRendezvous);
  1.1548 +
  1.1549 +	//This will be calling through CustomSecurityCheckL (server1 will return
  1.1550 +	//pass), but there is no easy way to determine that it has followed the
  1.1551 +	//correct path.
  1.1552 +	TInt r = Session.Connect();
  1.1553 +	test(r==KErrNone);
  1.1554 +
  1.1555 +	server.Terminate(KErrGeneral);
  1.1556 +	Session.Close();
  1.1557 +	CLOSE_AND_WAIT(server);
  1.1558 +	}
  1.1559 +
  1.1560 +void TestServer2WithPolicy8()
  1.1561 +	{
  1.1562 +	test.Next(_L("Server 2, Policy 8"));
  1.1563 +	RTestProcess server;
  1.1564 +	TRequestStatus rendezvous;
  1.1565 +	TRequestStatus logon;
  1.1566 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy8,ETestServer2);
  1.1567 +	server.Rendezvous(rendezvous);
  1.1568 +	server.Logon(logon);
  1.1569 +	server.Resume();
  1.1570 +	User::WaitForRequest(rendezvous);
  1.1571 +	test(rendezvous==KServerRendezvous);
  1.1572 +
  1.1573 +	TInt r = Session.Connect();
  1.1574 +	test(r==KErrServerTerminated);
  1.1575 +
  1.1576 +	//This is a simple way of testing that CustomSecurityCheckL is called for a
  1.1577 +	//connect custom check.  Server2 doesn't have an implementation of CustomSecurityCheckL
  1.1578 +	User::WaitForRequest(logon);
  1.1579 +	test(logon == EPolSvrCallingBaseImplementation);
  1.1580 +
  1.1581 +	Session.Close();
  1.1582 +	CLOSE_AND_WAIT(server);
  1.1583 +	}
  1.1584 +
  1.1585 +void TestServer3WithPolicy8()
  1.1586 +	{
  1.1587 +	test.Next(_L("Server 3, Policy 8"));
  1.1588 +	RTestProcess server;
  1.1589 +	TRequestStatus rendezvous;
  1.1590 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy8,ETestServer1);
  1.1591 +	server.Rendezvous(rendezvous);
  1.1592 +	server.Resume();
  1.1593 +	User::WaitForRequest(rendezvous);
  1.1594 +	test(rendezvous==KServerRendezvous);
  1.1595 +
  1.1596 +	//This will be calling through CustomSecurityCheckL
  1.1597 +	//(server3::CustomSecurityCheckL will fail this, but set the action to
  1.1598 +	//EQueryUser, which will call CustomFailureActionL which should will in
  1.1599 +	//this case pass) but there is no easy way to determine that it has
  1.1600 +	//followed the correct path.
  1.1601 +	TInt r = Session.Connect();
  1.1602 +	test(r==KErrNone);
  1.1603 +
  1.1604 +	//This policy doesn't have any IPC's that work.  Only way to shutdown
  1.1605 +	//server is to kill it.
  1.1606 +	server.Terminate(KErrGeneral);
  1.1607 +	Session.Close();
  1.1608 +	CLOSE_AND_WAIT(server);
  1.1609 +	}
  1.1610 +
  1.1611 +
  1.1612 +void TestServer1WithPolicy9()
  1.1613 +	{
  1.1614 +	test.Next(_L("Server 1, Policy 9"));
  1.1615 +	RTestProcess server;
  1.1616 +	TRequestStatus rendezvous;
  1.1617 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy9,ETestServer1);
  1.1618 +	server.Rendezvous(rendezvous);
  1.1619 +	server.Resume();
  1.1620 +	User::WaitForRequest(rendezvous);
  1.1621 +	test(rendezvous==KServerRendezvous);
  1.1622 +
  1.1623 +	TInt r = Session.Connect();
  1.1624 +	test(r==KErrPermissionDenied);
  1.1625 +
  1.1626 +	//We can do this because we have power management
  1.1627 +	server.Terminate(KErrGeneral);
  1.1628 +	CLOSE_AND_WAIT(server);
  1.1629 +	}
  1.1630 +
  1.1631 +void TestServer2WithPolicy1()
  1.1632 +	{	
  1.1633 +	test.Next(_L("Server 2, Policy 1"));
  1.1634 +	RTestProcess server;
  1.1635 +	TRequestStatus rendezvous;
  1.1636 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy1,ETestServer2);
  1.1637 +	server.Rendezvous(rendezvous);
  1.1638 +	server.Resume();
  1.1639 +	User::WaitForRequest(rendezvous);
  1.1640 +	test(rendezvous==KServerRendezvous);
  1.1641 +
  1.1642 +	TInt r = Session.Connect();
  1.1643 +	test(r==KErrNone);
  1.1644 +
  1.1645 +	TBuf8<4> flags(4);
  1.1646 +	r = Session.Send(CTestSession2::EShutdown, TIpcArgs(&flags));
  1.1647 +	test(r == KErrNone);
  1.1648 +	Session.Close();
  1.1649 +	CLOSE_AND_WAIT(server);
  1.1650 +	}
  1.1651 +
  1.1652 +void TestServer4WithPolicy10()
  1.1653 +	{	
  1.1654 +	test.Next(_L("Server 4, Policy 10"));
  1.1655 +	RTestProcess server;
  1.1656 +	TRequestStatus rendezvous;
  1.1657 +	server.Create(~KTestCapabilities,ETestProcessPolicyServer,EPolicy10,ETestServer4);
  1.1658 +	server.Rendezvous(rendezvous);
  1.1659 +	server.Resume();
  1.1660 +	User::WaitForRequest(rendezvous);
  1.1661 +	test(rendezvous==KServerRendezvous);
  1.1662 +
  1.1663 +	TInt r = Session.Connect();
  1.1664 +	test(r==KErrNone);
  1.1665 +
  1.1666 +	TBuf8<4> flags(4);
  1.1667 +
  1.1668 +	//case 0: Custom Check, fails with KErrPermissionDenied
  1.1669 +	SetFlags(flags, 0);
  1.1670 +	r = Session.Send(0, TIpcArgs(&flags));
  1.1671 +	test(r==KErrPermissionDenied);
  1.1672 +	test(FlagsValue(flags) == (0 | KCustomCheckMask | KActiveCheckMask | KActiveRunLMask) ); 
  1.1673 +
  1.1674 +	//case 1: Custom Check passes.
  1.1675 +	SetFlags(flags, 1);
  1.1676 +	r = Session.Send(1, TIpcArgs(&flags));
  1.1677 +	test(r==KErrNone);
  1.1678 +	test(FlagsValue(flags) == (1 | KCustomCheckMask | KActiveCheckMask | KActiveRunLMask | KServiceLMask) ); 
  1.1679 +
  1.1680 +	//case 2: Custom Check fails but action set to EQueryUser, query
  1.1681 +	//subsequently fails
  1.1682 +	SetFlags(flags, 2);
  1.1683 +	r = Session.Send(2, TIpcArgs(&flags));
  1.1684 +	test(r==KErrPermissionDenied);
  1.1685 +	test(FlagsValue(flags) == (2 | KCustomCheckMask | KActiveCheckMask | KActiveRunLMask | KCustomActionMask | KActiveActionMask ));
  1.1686 +
  1.1687 +	//case 3: Custom Check fails but action set to EQueryUser, query
  1.1688 +	//subsequently passes
  1.1689 +	SetFlags(flags, 3);
  1.1690 +	r = Session.Send(3, TIpcArgs(&flags));
  1.1691 +	test(r==KErrNone);
  1.1692 +	test(FlagsValue(flags) == (3 | KCustomCheckMask | KActiveCheckMask | KActiveRunLMask | KCustomActionMask | KActiveActionMask | KServiceLMask ));
  1.1693 +	
  1.1694 +	//case 4: Custom Check passes and action is set.  Action set shouldn't make
  1.1695 +	//a difference.  Should be same result as case 1.
  1.1696 +	SetFlags(flags, 4);
  1.1697 +	r = Session.Send(4, TIpcArgs(&flags));
  1.1698 +	test(r==KErrNone);
  1.1699 +	test(FlagsValue(flags) == (4 | KCustomCheckMask | KActiveCheckMask | KActiveRunLMask | KServiceLMask) );
  1.1700 +
  1.1701 +	//case 5: Always passes at the policy server level.
  1.1702 +	SetFlags(flags, 5);
  1.1703 +	r = Session.Send(5, TIpcArgs(&flags));
  1.1704 +	test(r==KErrNone);
  1.1705 +	test(FlagsValue(flags) == (5 | KServiceLMask) );
  1.1706 +
  1.1707 +	//case 6: Not Supported, returned from policy server level.
  1.1708 +	SetFlags(flags, 6);
  1.1709 +	r = Session.Send(6, TIpcArgs(&flags));
  1.1710 +	test(r==KErrNotSupported);
  1.1711 +	test(FlagsValue(flags) == (6));
  1.1712 +
  1.1713 +	//case 7: Not Supported, returned from policy server level.
  1.1714 +	SetFlags(flags, 7);
  1.1715 +	r = Session.Send(7, TIpcArgs(&flags));
  1.1716 +	test(r==KErrNotSupported);
  1.1717 +	test(FlagsValue(flags) == (7));
  1.1718 +
  1.1719 +	//case 8: Requires NetworkControl, which we have, so it passes.
  1.1720 +	SetFlags(flags, 8);
  1.1721 +	r = Session.Send(8, TIpcArgs(&flags));
  1.1722 +	test(r==KErrNone);
  1.1723 +	test(FlagsValue(flags) == (8 | KServiceLMask));
  1.1724 +
  1.1725 +	//case 9: Requires DiskAdmin, which we don't have. EQueryUser is set, and
  1.1726 +	//it passes.
  1.1727 +	SetFlags(flags, 9);
  1.1728 +	r = Session.Send(9, TIpcArgs(&flags));
  1.1729 +	test(r==KErrNone);
  1.1730 +	test(FlagsValue(flags) == (9 | KCustomActionMask | KActiveActionMask | KActiveRunLMask | KServiceLMask));
  1.1731 +
  1.1732 +	//case 10: Requires DiskAdmin which we don't have.  
  1.1733 +	SetFlags(flags, 10);
  1.1734 +	r = Session.Send(10, TIpcArgs(&flags));
  1.1735 +	test(r==KErrPermissionDenied);
  1.1736 +	test(FlagsValue(flags) == (10));
  1.1737 +
  1.1738 +	//case 11: Requires NetworkControl -> pass.  Thrown in a EQueryUser to see
  1.1739 +	//if it causes any problems -> it shouldn't.  Should be same as case 8.
  1.1740 +	SetFlags(flags, 11);
  1.1741 +	r = Session.Send(11, TIpcArgs(&flags));
  1.1742 +	test(r==KErrNone);
  1.1743 +	test(FlagsValue(flags) == (11 | KServiceLMask));
  1.1744 +
  1.1745 +	//case 12: Not Supported, returned from policy server level.
  1.1746 +	SetFlags(flags, 12);
  1.1747 +	r = Session.Send(12, TIpcArgs(&flags));
  1.1748 +	test(r==KErrNotSupported);
  1.1749 +	test(FlagsValue(flags) == (12));
  1.1750 +
  1.1751 +	//case 13: Not Supported, returned from policy server level.
  1.1752 +	SetFlags(flags, 13);
  1.1753 +	r = Session.Send(13, TIpcArgs(&flags));
  1.1754 +	test(r==KErrNotSupported);
  1.1755 +	test(FlagsValue(flags) == (13));
  1.1756 +
  1.1757 +	//case 54: Not Supported, returned from policy server level.
  1.1758 +	SetFlags(flags, 54);
  1.1759 +	r = Session.Send(54, TIpcArgs(&flags));
  1.1760 +	test(r==KErrNotSupported);
  1.1761 +	test(FlagsValue(flags) == (54));
  1.1762 +
  1.1763 +	//case 55: Requires DiskAdmin -> Fail.  But then we query user, and then we
  1.1764 +	//cancel that.
  1.1765 +	SetFlags(flags, 55);
  1.1766 +	r = Session.Send(55, TIpcArgs(&flags));
  1.1767 +	test(r==KErrCancel);
  1.1768 +	test(FlagsValue(flags) == (55 | KCustomActionMask | KActiveActionMask | KActiveDoCancelMask));
  1.1769 +
  1.1770 +	//case 56: Requires DiskAdmin -> Fail.  But then we query user which leaves.
  1.1771 +	SetFlags(flags, 56);
  1.1772 +	r = Session.Send(56, TIpcArgs(&flags));
  1.1773 +	test(r==KErrNoMemory);
  1.1774 +	test(FlagsValue(flags) == (56 | KCustomActionMask | KActiveActionMask | KActiveRunLMask | KActiveRunErrorMask));
  1.1775 +
  1.1776 +	//case 57: Requires DiskAdmin -> Fail.  But then we query user which passes
  1.1777 +	//and then we leave in the ServiceL
  1.1778 +	SetFlags(flags, 57);
  1.1779 +	r = Session.Send(57, TIpcArgs(&flags));
  1.1780 +	test(r==KErrNoMemory);
  1.1781 +	test(FlagsValue(flags) == (57 | KCustomActionMask | KActiveActionMask | KActiveRunLMask | KServiceLMask | KActiveRunErrorMask | KServiceErrorMask ));
  1.1782 +
  1.1783 +	//case 58: Not Supported, returned from policy server level.
  1.1784 +	SetFlags(flags, 58);
  1.1785 +	r = Session.Send(58, TIpcArgs(&flags));
  1.1786 +	test(r==KErrNotSupported);
  1.1787 +	test(FlagsValue(flags) == (58));
  1.1788 +
  1.1789 +	//case 86: Not Supported, returned from policy server level.
  1.1790 +	SetFlags(flags, 86);
  1.1791 +	r = Session.Send(86, TIpcArgs(&flags));
  1.1792 +	test(r==KErrNotSupported);
  1.1793 +	test(FlagsValue(flags) == (86));
  1.1794 +
  1.1795 +	//case 99: Not Supported, returned from policy server level.
  1.1796 +	SetFlags(flags, 99);
  1.1797 +	r = Session.Send(99, TIpcArgs(&flags));
  1.1798 +	test(r==KErrNotSupported);
  1.1799 +	test(FlagsValue(flags) == (99));
  1.1800 +
  1.1801 +	//case 101: Not Supported, returned from policy server level.
  1.1802 +	SetFlags(flags, 101);
  1.1803 +	r = Session.Send(101, TIpcArgs(&flags));
  1.1804 +	test(r==KErrNotSupported);
  1.1805 +	test(FlagsValue(flags) == (101));
  1.1806 +
  1.1807 +	//case 5000: Not Supported, returned from policy server level.
  1.1808 +	SetFlags(flags, 5000);
  1.1809 +	r = Session.Send(5000, TIpcArgs(&flags));
  1.1810 +	test(r==KErrNotSupported);
  1.1811 +	test(FlagsValue(flags) == (5000));
  1.1812 +
  1.1813 +	//case KMaxTInt-1: Not Supported, returned from policy server level.
  1.1814 +	SetFlags(flags, KMaxTInt-1);
  1.1815 +	r = Session.Send(KMaxTInt-1, TIpcArgs(&flags));
  1.1816 +	test(r==KErrNotSupported);
  1.1817 +	test(FlagsValue(flags) == (KMaxTInt-1));
  1.1818 +
  1.1819 +	//case KMaxTInt: Always pass from policy framework
  1.1820 +	//This also tests another exit condition from the binary search
  1.1821 +	SetFlags(flags, 0);
  1.1822 +	r = Session.Send(KMaxTInt, TIpcArgs(&flags));
  1.1823 +	//Instead of KErrNone we return KMaxTInt as we can't fit the KMaxTInt in
  1.1824 +	//the flags without overwriting stuff
  1.1825 +	test(r==KMaxTInt);
  1.1826 +	test(FlagsValue(flags) == (0 | KServiceLMask ));
  1.1827 +
  1.1828 +	r = Session.Send(CTestSession2::EShutdown, TIpcArgs(&flags));
  1.1829 +	test(r == KErrNone);
  1.1830 +	Session.Close();
  1.1831 +	CLOSE_AND_WAIT(server);
  1.1832 +	}
  1.1833 +
  1.1834 +GLDEF_C TInt E32Main()
  1.1835 +    {
  1.1836 +	TBuf16<512> cmd;
  1.1837 +	User::CommandLine(cmd);
  1.1838 +	if(cmd.Length() && TChar(cmd[0]).IsDigit())
  1.1839 +		{
  1.1840 +		TInt function = -1;
  1.1841 +		TInt arg1 = -1;
  1.1842 +		TInt arg2 = -1;
  1.1843 +		TLex lex(cmd);
  1.1844 +
  1.1845 +		lex.Val(function);
  1.1846 +		lex.SkipSpace();
  1.1847 +		lex.Val(arg1);
  1.1848 +		lex.SkipSpace();
  1.1849 +		lex.Val(arg2);
  1.1850 +		return DoTestProcess(function,arg1,arg2);
  1.1851 +		}
  1.1852 +
  1.1853 +	test.Title();
  1.1854 +
  1.1855 +	if(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement))
  1.1856 +		{
  1.1857 +		test.Start(_L("TESTS NOT RUN - EPlatSecEnforcement is OFF"));
  1.1858 +		test.End();
  1.1859 +		return 0;
  1.1860 +		}
  1.1861 +
  1.1862 +	test.Start(_L("Policy Server Tests"));
  1.1863 +
  1.1864 +	/* Server1 has implementations of CustomSecurityCheckL and
  1.1865 +	CustomFailureActionL This is the test for all the paths in a policy servers
  1.1866 +	implementations -- not all connect paths though
  1.1867 +	This test also ensures that every path through the binary search is
  1.1868 +	covered. */
  1.1869 +	TestServer1WithPolicy1();
  1.1870 +
  1.1871 +	/* Policy2,3,4,5,6, are bad policies that should cause the server to panic
  1.1872 +	in debug mode.  In release, they'll just pass, however, if you later tried
  1.1873 +	to use them, something would go horribly wrong */
  1.1874 +	TestServer1WithPolicy2();
  1.1875 +	TestServer1WithPolicy3();
  1.1876 +	TestServer1WithPolicy4();
  1.1877 +	TestServer1WithPolicy5();
  1.1878 +	TestServer1WithPolicy6();
  1.1879 +
  1.1880 +	/* Policies 7,8,9 check various types of connect policies. */
  1.1881 +	TestServer1WithPolicy7();
  1.1882 +	// Server 1,2,3 are used here all with policy 8 because you can only test 1
  1.1883 +	// type of connect policy per server.
  1.1884 +	TestServer1WithPolicy8();
  1.1885 +	TestServer2WithPolicy8();
  1.1886 +	TestServer3WithPolicy8();
  1.1887 +	TestServer1WithPolicy9();
  1.1888 +
  1.1889 +	/* Sever2 does not have implementations of CustomSecurityCheckL and
  1.1890 +	CustomFailureActionL. When these functions are called it should crash */
  1.1891 +	TestServer2WithPolicy1();
  1.1892 +
  1.1893 +	/* Server4 is used for checking what happens when the custom functions use
  1.1894 +	another active object.  This test encompasses leaving due to OOM, and
  1.1895 +	cancellation. */
  1.1896 +	TestServer4WithPolicy10();
  1.1897 +
  1.1898 +	test.End();
  1.1899 +	return(0);
  1.1900 +    }
  1.1901 +