os/kernelhwsrv/kerneltest/e32test/property/t_security_slave.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/property/t_security_slave.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,393 @@
     1.4 +// Copyright (c) 2002-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 +//
    1.18 +
    1.19 +#include <e32test.h>
    1.20 +#include "t_property.h"
    1.21 +
    1.22 +TSecureId TestSecureId;
    1.23 +TVendorId TestVendorId;
    1.24 +TCapability TestCaps[ECapability_Limit];
    1.25 +TCapability TestMissingCaps[ECapability_Limit];
    1.26 +
    1.27 +TSecurityPolicy CorrectCapPolicy;
    1.28 +TSecurityPolicy IncorrectCapPolicy;
    1.29 +
    1.30 +TUint TestPolicyCount=0;
    1.31 +
    1.32 +#include <e32svr.h>
    1.33 +
    1.34 +TBool MakeTestSecurityPolicy()
    1.35 +	{
    1.36 +	TCapability* c =TestCaps;
    1.37 +	TCapability* m =TestMissingCaps;
    1.38 +	TUint t=++TestPolicyCount;
    1.39 +	RDebug::Print(_L("Test policy %d\n"),t);
    1.40 +	if(t==1)
    1.41 +		{
    1.42 +		_LIT_SECURITY_POLICY_PASS(KSecurityPolicyPass);
    1.43 +		CorrectCapPolicy = KSecurityPolicyPass;
    1.44 +		_LIT_SECURITY_POLICY_FAIL(KSecurityPolicyFail);
    1.45 +		IncorrectCapPolicy = KSecurityPolicyFail;
    1.46 +		return ETrue;
    1.47 +		}
    1.48 +	t -= 1;
    1.49 +	if(t<7)
    1.50 +		{
    1.51 +		switch(t)
    1.52 +			{
    1.53 +		case 0:
    1.54 +			CorrectCapPolicy = TSecurityPolicy(c[0]);
    1.55 +			IncorrectCapPolicy = TSecurityPolicy(m[0]);
    1.56 +			break;
    1.57 +		case 1:
    1.58 +			CorrectCapPolicy = TSecurityPolicy(c[0],c[1]);
    1.59 +			IncorrectCapPolicy = TSecurityPolicy(m[0],c[1]);
    1.60 +			break;
    1.61 +		case 2:
    1.62 +			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2]);
    1.63 +			IncorrectCapPolicy = TSecurityPolicy(c[0],m[1],c[2]);
    1.64 +			break;
    1.65 +		case 3:
    1.66 +			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3]);
    1.67 +			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],m[2],c[3]);
    1.68 +			break;
    1.69 +		case 4:
    1.70 +			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4]);
    1.71 +			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],m[3],c[4]);
    1.72 +			break;
    1.73 +		case 5:
    1.74 +			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4],c[5]);
    1.75 +			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],m[4],c[5]);
    1.76 +			break;
    1.77 +		case 6:
    1.78 +			CorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4],c[5],c[6]);
    1.79 +			IncorrectCapPolicy = TSecurityPolicy(c[0],c[1],c[2],c[3],c[4],m[5],c[6]);
    1.80 +			break;
    1.81 +			}
    1.82 +		return ETrue;
    1.83 +		}
    1.84 +	t -= 7;
    1.85 +	if(t<4)
    1.86 +		{
    1.87 +		TSecureId id = TestSecureId;
    1.88 +		TSecureId id2 = TSecureId((TUint32)TestVendorId);
    1.89 +		switch(t)
    1.90 +			{
    1.91 +		case 0:
    1.92 +			CorrectCapPolicy = TSecurityPolicy(id);
    1.93 +			IncorrectCapPolicy = TSecurityPolicy(id2);
    1.94 +			break;
    1.95 +		case 1:
    1.96 +			CorrectCapPolicy = TSecurityPolicy(id,c[0]);
    1.97 +			IncorrectCapPolicy = TSecurityPolicy(id2,c[0]);
    1.98 +			break;
    1.99 +		case 2:
   1.100 +			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1]);
   1.101 +			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1]);
   1.102 +			break;
   1.103 +		case 3:
   1.104 +			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1],c[2]);
   1.105 +			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1],c[2]);
   1.106 +			break;
   1.107 +			}
   1.108 +		return ETrue;
   1.109 +		}
   1.110 +	t -= 4;
   1.111 +	if(t<4)
   1.112 +		{
   1.113 +		TVendorId id = TestVendorId;
   1.114 +		TVendorId id2 = TVendorId((TUint32)TestSecureId);
   1.115 +		switch(t)
   1.116 +			{
   1.117 +		case 0:
   1.118 +			CorrectCapPolicy = TSecurityPolicy(id);
   1.119 +			IncorrectCapPolicy = TSecurityPolicy(id2);
   1.120 +			break;
   1.121 +		case 1:
   1.122 +			CorrectCapPolicy = TSecurityPolicy(id,c[0]);
   1.123 +			IncorrectCapPolicy = TSecurityPolicy(id2,c[0]);
   1.124 +			break;
   1.125 +		case 2:
   1.126 +			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1]);
   1.127 +			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1]);
   1.128 +			break;
   1.129 +		case 3:
   1.130 +			CorrectCapPolicy = TSecurityPolicy(id,c[0],c[1],c[2]);
   1.131 +			IncorrectCapPolicy = TSecurityPolicy(id2,c[0],c[1],c[2]);
   1.132 +			break;
   1.133 +			}
   1.134 +		return ETrue;
   1.135 +		}
   1.136 +	t -= 4;
   1.137 +	TestPolicyCount = 0;
   1.138 +	return EFalse;
   1.139 +	}
   1.140 +
   1.141 +_LIT(KSecurityReadCapabilityName, "RProperty Security: Read Capability Basics");
   1.142 +
   1.143 +class CPropSecurityReadCapability : public CTestProgram
   1.144 +	{
   1.145 +public:
   1.146 +	CPropSecurityReadCapability(TUid aCategory, TUint aKey1, TUint aKey2, RProperty::TType aType) :
   1.147 +		CTestProgram(KSecurityReadCapabilityName), iCategory(aCategory), iKey1(aKey1), iKey2(aKey2), iType(aType)
   1.148 +		{
   1.149 +		}
   1.150 +
   1.151 +	void Run(TUint aCount);
   1.152 +
   1.153 +private:
   1.154 +	TUid				iCategory;
   1.155 +	TUint				iKey1;
   1.156 +	TUint				iKey2;
   1.157 +	RProperty::TType	iType;
   1.158 +	};
   1.159 +
   1.160 +void CPropSecurityReadCapability::Run(TUint aCount)
   1.161 +	{
   1.162 +	while(MakeTestSecurityPolicy())
   1.163 +	for (TUint i = 0; i < aCount; ++i)
   1.164 +		{
   1.165 +		RProperty	prop1;
   1.166 +		RProperty	prop2;
   1.167 +
   1.168 +		TInt r = prop1.Attach(iCategory, iKey1);
   1.169 +		TF_ERROR(r, r == KErrNone);
   1.170 +		r = prop2.Attach(iCategory, iKey2);
   1.171 +		TF_ERROR(r, r == KErrNone);
   1.172 +
   1.173 +		// If the property has not been defined, the request will not complete until the property
   1.174 +		// is defined and published.
   1.175 +		// When defined if the caller does not have read capabilities the request completes
   1.176 +		// with KErrPermissionDenied.
   1.177 +		TRequestStatus status1;
   1.178 +		prop1.Subscribe(status1);
   1.179 +		TF_ERROR(status1.Int(), status1.Int() == KRequestPending);
   1.180 +		r = prop1.Define(iCategory, iKey1, iType, IncorrectCapPolicy, CorrectCapPolicy);
   1.181 +		TF_ERROR(r, r == KErrNone);
   1.182 +		User::WaitForRequest(status1);
   1.183 +		TF_ERROR(status1.Int(), status1.Int() == KErrPermissionDenied);
   1.184 +
   1.185 +		TRequestStatus status2;
   1.186 +		prop2.Subscribe(status2);
   1.187 +		TF_ERROR(status2.Int(), status2.Int() == KRequestPending);
   1.188 +		r = prop2.Define(iCategory, iKey2, iType, CorrectCapPolicy, IncorrectCapPolicy);
   1.189 +		TF_ERROR(r, r == KErrNone);
   1.190 +		TF_ERROR(status2.Int(), status2.Int() == KRequestPending);
   1.191 +		prop2.Cancel();
   1.192 +		User::WaitForRequest(status2);
   1.193 +		TF_ERROR(status2.Int(), status2.Int() == KErrCancel);
   1.194 +
   1.195 +		// If the Subscribe() caller does not have the read capabilities the request completes immediately
   1.196 +		// with KErrPermissionDenied.
   1.197 +		prop1.Subscribe(status1);
   1.198 +		User::WaitForRequest(status1);
   1.199 +		TF_ERROR(status1.Int(), status1.Int() == KErrPermissionDenied);
   1.200 +
   1.201 +		prop2.Subscribe(status2);
   1.202 +		TF_ERROR(status2.Int(), status2.Int() == KRequestPending);
   1.203 +		prop2.Cancel();
   1.204 +		User::WaitForRequest(status2);
   1.205 +		TF_ERROR(status2.Int(), status2.Int() == KErrCancel);
   1.206 +
   1.207 +		// If the caller does not have the read capabilities Get() fails with KErrPermissionDenied.
   1.208 +		if (iType == RProperty::EInt)
   1.209 +			{
   1.210 +			TInt value;
   1.211 +			r = prop1.Get(iCategory, iKey1, value);
   1.212 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.213 +			r = prop2.Get(iCategory, iKey2, value);
   1.214 +			TF_ERROR(r, r == KErrNone);
   1.215 +			r = prop1.Get(value);
   1.216 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.217 +			r = prop2.Get(value);
   1.218 +			TF_ERROR(r, r == KErrNone);
   1.219 +			}
   1.220 +		else 
   1.221 +			{
   1.222 +			TBuf<16> buf;
   1.223 +			TBuf8<16> buf8;
   1.224 +			r = prop1.Get(iCategory, iKey1, buf);
   1.225 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.226 +			r = prop2.Get(iCategory, iKey2, buf);
   1.227 +			TF_ERROR(r, r == KErrNone);
   1.228 +			r = prop1.Get(iCategory, iKey1, buf8);
   1.229 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.230 +			r = prop2.Get(iCategory, iKey2, buf8);
   1.231 +			TF_ERROR(r, r == KErrNone);
   1.232 +			r = prop1.Get(buf);
   1.233 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.234 +			r = prop2.Get(buf);
   1.235 +			TF_ERROR(r, r == KErrNone);
   1.236 +			r = prop1.Get(buf8);
   1.237 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.238 +			r = prop2.Get(buf8);
   1.239 +			TF_ERROR(r, r == KErrNone);
   1.240 +			}
   1.241 +
   1.242 +		r = prop1.Delete(iCategory, iKey1);
   1.243 +		TF_ERROR(r, r == KErrNone);
   1.244 +		r = prop2.Delete(iCategory, iKey2);
   1.245 +		TF_ERROR(r, r == KErrNone);
   1.246 +		prop1.Close();
   1.247 +		prop2.Close();
   1.248 +		}
   1.249 +	}
   1.250 +
   1.251 +_LIT(KSecurityWriteCapabilityName, "RProperty Security: Write Cpability Basics");
   1.252 +
   1.253 +class CPropSecurityWriteCapability : public CTestProgram
   1.254 +	{
   1.255 +public:
   1.256 +	CPropSecurityWriteCapability(TUid aCategory, TUint aKey1, TUint aKey2, RProperty::TType aType) :
   1.257 +		CTestProgram(KSecurityWriteCapabilityName), iCategory(aCategory), iKey1(aKey1), iKey2(aKey2), iType(aType)
   1.258 +		{
   1.259 +		}
   1.260 +
   1.261 +	void Run(TUint aCount);
   1.262 +
   1.263 +private:
   1.264 +	TUid				iCategory;
   1.265 +	TUint				iKey1;
   1.266 +	TUint				iKey2;
   1.267 +	RProperty::TType	iType;
   1.268 +	};
   1.269 +
   1.270 +void CPropSecurityWriteCapability::Run(TUint aCount)
   1.271 +	{
   1.272 +	while(MakeTestSecurityPolicy())
   1.273 +	for (TUint i = 0; i < aCount; ++i)
   1.274 +		{
   1.275 +		RProperty	prop1;
   1.276 +		RProperty	prop2;
   1.277 +
   1.278 +		TInt r = prop1.Attach(iCategory, iKey1);
   1.279 +		TF_ERROR(r, r == KErrNone);
   1.280 +		r = prop2.Attach(iCategory, iKey2);
   1.281 +		TF_ERROR(r, r == KErrNone);
   1.282 +
   1.283 +		r = prop1.Define(iCategory, iKey1, iType, CorrectCapPolicy, IncorrectCapPolicy);
   1.284 +		TF_ERROR(r, r == KErrNone);
   1.285 +		r = prop2.Define(iCategory, iKey2, iType, IncorrectCapPolicy, CorrectCapPolicy);
   1.286 +		TF_ERROR(r, r == KErrNone);
   1.287 +
   1.288 +		// If the caller does not have the write capabilities Set() fails with KErrPermissionDenied.
   1.289 +		if (iType == RProperty::EInt)
   1.290 +			{
   1.291 +			TInt value = 1;
   1.292 +			r = prop1.Set(iCategory, iKey1, value);
   1.293 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.294 +			r = prop2.Set(iCategory, iKey2, value);
   1.295 +			TF_ERROR(r, r == KErrNone);
   1.296 +			r = prop1.Set(value);
   1.297 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.298 +			r = prop2.Set(value);
   1.299 +			TF_ERROR(r, r == KErrNone);
   1.300 +			}
   1.301 +		else 
   1.302 +			{
   1.303 +			TBuf<16> buf(_L("Foo"));
   1.304 +			TBuf8<16> buf8((TUint8*) "Foo");
   1.305 +			r = prop1.Set(iCategory, iKey1, buf);
   1.306 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.307 +			r = prop2.Set(iCategory, iKey2, buf);
   1.308 +			TF_ERROR(r, r == KErrNone);
   1.309 +			r = prop1.Set(iCategory, iKey1, buf8);
   1.310 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.311 +			r = prop2.Set(iCategory, iKey2, buf8);
   1.312 +			TF_ERROR(r, r == KErrNone);
   1.313 +			r = prop1.Set(buf);
   1.314 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.315 +			r = prop2.Set(buf);
   1.316 +			TF_ERROR(r, r == KErrNone);
   1.317 +			r = prop1.Set(buf8);
   1.318 +			TF_ERROR(r, r == KErrPermissionDenied);
   1.319 +			r = prop2.Set(buf8);
   1.320 +			TF_ERROR(r, r == KErrNone);
   1.321 +			}
   1.322 +		
   1.323 +		r = prop1.Delete(iCategory, iKey1);
   1.324 +		TF_ERROR(r, r == KErrNone);
   1.325 +		r = prop2.Delete(iCategory, iKey2);
   1.326 +		TF_ERROR(r, r == KErrNone);
   1.327 +		prop1.Close();
   1.328 +		prop2.Close();
   1.329 +		}
   1.330 +	}
   1.331 +
   1.332 +GLDEF_C TInt E32Main()
   1.333 +	{
   1.334 +
   1.335 +	TSecurityInfo info;
   1.336 +	info.Set(RProcess());
   1.337 +	TestSecureId = info.iSecureId;
   1.338 +	TestVendorId = info.iVendorId;
   1.339 +	{
   1.340 +	TInt c=0;
   1.341 +	TInt m=0;
   1.342 +	for(TInt i=0; i<ECapability_Limit; i++)
   1.343 +		{
   1.344 +		if(info.iCaps.HasCapability((TCapability)i))
   1.345 +			TestCaps[c++] = (TCapability)i;
   1.346 +		else
   1.347 +			TestMissingCaps[m++] = (TCapability)i;
   1.348 +		}
   1.349 +	__ASSERT_ALWAYS(c>=7,User::Panic(_L("not enough caps"), 1));
   1.350 +	__ASSERT_ALWAYS(m>=6,User::Panic(_L("missing caps <6"), 1));
   1.351 +	}
   1.352 +		
   1.353 +	TInt len = User::CommandLineLength();
   1.354 +	__ASSERT_ALWAYS(len, User::Panic(_L("t_prop_sec: bad args"), 0));
   1.355 +
   1.356 +	// Get arguments for the command line
   1.357 +	TInt size = len * sizeof(TUint16);
   1.358 +	HBufC8* hb = HBufC8::NewMax(size);
   1.359 +	__ASSERT_ALWAYS(hb, User::Panic(_L("t_prop_sec: no memory"), 0));
   1.360 +	TPtr cmd((TUint16*) hb->Ptr(), len);
   1.361 +	User::CommandLine(cmd);
   1.362 +	CPropSecurity::TArgs* args = (CPropSecurity::TArgs*) hb->Ptr();	
   1.363 +
   1.364 +	CTestProgram::Start();
   1.365 +
   1.366 +	CTestProgram* progs[] = 
   1.367 +		{ 
   1.368 +		new CPropSecurityReadCapability(args->iCategory, args->iSlaveKeySlot + 0, args->iSlaveKeySlot + 1, 
   1.369 +										RProperty::EInt),
   1.370 +		new CPropSecurityReadCapability(args->iCategory, args->iSlaveKeySlot + 2, args->iSlaveKeySlot + 3, 
   1.371 +										RProperty::EByteArray),
   1.372 +		new CPropSecurityWriteCapability(args->iCategory, args->iSlaveKeySlot + 4, args->iSlaveKeySlot + 5, 
   1.373 +										RProperty::EInt),
   1.374 +		new CPropSecurityWriteCapability(args->iCategory, args->iSlaveKeySlot + 6, args->iSlaveKeySlot + 7, 
   1.375 +										RProperty::EByteArray),
   1.376 +		NULL 
   1.377 +		};
   1.378 +
   1.379 +	TInt i;
   1.380 +	TInt n = (sizeof(progs)/sizeof(*progs)) - 1;
   1.381 +	for (i = 0; i < n; ++i)
   1.382 +		{
   1.383 +		__ASSERT_ALWAYS(progs[i], User::Panic(_L("t_property: no memory"), 0));
   1.384 +		}
   1.385 +	
   1.386 +	CTestProgram::LaunchGroup(progs, 2);
   1.387 +
   1.388 +	for (i = 0; i < n; ++i)
   1.389 +		{
   1.390 +		delete progs[i];
   1.391 +		}
   1.392 +
   1.393 +	CTestProgram::End();
   1.394 +
   1.395 +	return KErrNone;
   1.396 +	}